How to interpolate multiple variables in a Markdown string in Julia

Viewed 401

In the following Julia 1.5 code:

a, b = 4, 5
"a=$(a), b=($b)" # "a=4, b=5"
using Markdown
md"a=$(a), b=($b)" # a=a, b=b
# but...
Markdown.parse("a=$(a), b=($b)") # "a=4, b=5"

It seems that the Markdown macro thinks two $ indicate a math expression. But the parse handles it OK.

Can someone explain this? is there a way to use the md"..." form for this.

1 Answers

It's not obvious in my opinion, but I think $ with a non-space before is interpreted as a closing LaTeX $ if there is one before.

Some suggestions:

  • If you're OK with spaces around your = sign, then this works:

    julia> md"a = $a, b = $b"
      a = 4, b = 5
    
  • Or you could make it a list:

    julia> md"""
           - a=$a
           - b=$b
           """
        •    a=4
    
        •    b=5
    
Related