# '\\n' is not working in \`print\` for customized string literals

**URL:** <https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 26, 2019, 5:32am UTC](https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517 "2019-11-26T05:32:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [November 26, 2019, 5:32am UTC](https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517/1 "2019-11-26T05:32:47Z")

</div>

I define a `@c_str` macro for colored strings:

```julia
using Rematch: @match

function color_string(str::AbstractString, color::AbstractChar)
    env = @match color begin
        'b' => "\033[34m"
        'r' => "\033[31m"
        'g' => "\033[32m"
        'm' => "\033[35m"
        _ => throw(ArgumentError("unknown color flag: $color"))
    end
    return env * str * "\033[0m\033[0m"
end

macro c_str(str, color = 'g')
    @assert(length(color) == 1)
    return color_string(str, first(color))
end # macro c_str

```

However, `print` a `c_str` with `'\n'` inside does not print new lines anymore:

```julia
julia> print(c"new line\n"r)
new line\n
julia> print(c"\nnew line\n"r)
\nnew line\n
julia>

```

Can it be solved?

Also, string interpolation is not working:

```julia
julia> a = 1
1

julia> c" $a "
"\e[32m \$a \e[0m\e[0m"

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 26, 2019, 6:21am UTC](https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517/2 "2019-11-26T06:21:38Z")

</div>

For custom string literals, you get the raw string without processing of escape characters or interpolations. It’s up to you to implement those extra behaviors if desired:

```julia
julia> macro blah_str(str)
           for c in str
               println(repr(c))
           end
       end
@blah_str (macro with 1 method)

julia> blah"\n$x"
'\\'
'n'
'$'
'x'

```

Doing something like `Meta.parse('"' * replace(str, '"' => "\\\"") * '"')` inside the macro and processing the resulting string or AST can be a convenient way to recover the usual interpolation support.

---

<div class="post-metadata">

**Author:** ![arabidopsis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arabidopsis/32/26457_2.png) [@arabidopsis](https://discourse.julialang.org/u/arabidopsis)\
**Post date:** [March 9, 2022, 2:48am UTC](https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517/3 "2022-03-09T02:48:50Z")

</div>

This seems inconsistent with the documentation however. From [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals) viz:

> Like string literals, command literals can also be prefixed by an identifier to form what are called non-standard command literals. These command literals are parsed as calls to specially-named macros. For example, the syntax `custom`literal`` is parsed as `@custom_cmd "literal"` . Julia itself does not contain any non-standard command literals, but packages can make use of this syntax. Aside from the different syntax and the `_cmd` suffix instead of the `_str` suffix, non-standard command literals behave exactly like non-standard string literals.

_But_

```julia
macro blah_str(str)
    return quote
        @eval $str
    end
end

v = 1
@blah_str "$v" # => "1"
blah"$v" # => "\$v"

```

So they don’t seem to be parsed the same as each other (similarly for commands).

This is julia v1.7.1

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 9, 2022, 3:02am UTC](https://discourse.julialang.org/t/n-is-not-working-in-print-for-customized-string-literals/31517/4 "2022-03-09T03:02:17Z")

</div>

> [@arabidopsis](#):
>
> > Aside from the different syntax and the `_cmd` suffix instead of the `_str` suffix, non-standard command literals behave exactly like non-standard string literals

The point here is that `x"blah"` and `x`blah` ` are parsed the same (other than having different sufficies `@x_str` vs `@x_cmd`.)

However `@x_str "str"` and `x"str"` are not the same if `str` contains escape sequences; the manual doesn’t really discuss this case for nonstandard string literals. I don’t think the manual is inconsistent here but it is incomplete.
