Skip to content

Commit

Permalink
Fix minted/lstlistings escaping, fixes #1200.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Mar 22, 2020
1 parent 73dfaa8 commit 63de78d
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/Writers/LaTeXWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,19 +435,41 @@ function latex(io::IO, code::Markdown.Code)
language = isempty(code.language) ? "none" : code.language
# the julia-repl is called "jlcon" in Pygments
language = (language == "julia-repl") ? "jlcon" : language
escape = '' code.code
if language in LEXER
_print(io, "\n\\begin{minted}[escapeinside=\\%\\%]")
_print(io, "\n\\begin{minted}")
if escape
_print(io, "[escapeinside=\\#\\%]")
end
_println(io, "{", language, "}")
_print_code_escapes(io, code.code)
_println(io, "\\end{minted}\n")
if escape
_print_code_escapes_minted(io, code.code)
else
_print(io, code.code)
end
_println(io, "\n\\end{minted}\n")
else
_println(io, "\n\\begin{lstlisting}[escapeinside=\\%\\%]")
_print_code_escapes(io, code.code)
_println(io, "\\end{lstlisting}\n")
_print(io, "\n\\begin{lstlisting}")
if escape
_println(io, "[escapeinside=\\%\\%]")
_print_code_escapes_lstlisting(io, code.code)
else
_println(io)
_print(io, code.code)
end
_println(io, "\n\\end{lstlisting}\n")
end
end

function _print_code_escapes(io, s::AbstractString)
function _print_code_escapes_minted(io, s::AbstractString)
for ch in s
ch === '#' ? _print(io, "##%") :
ch === '%' ? _print(io, "#%%") : # Note: "#\\%%" results in pygmentize error...
ch === '' ? _print(io, "#\\unicodeveebar%") :
_print(io, ch)
end
end
function _print_code_escapes_lstlisting(io, s::AbstractString)
for ch in s
ch === '%' ? _print(io, "%\\%%") :
ch === '' ? _print(io, "%\\unicodeveebar%") :
Expand Down

0 comments on commit 63de78d

Please sign in to comment.