Documenter.jl icon indicating copy to clipboard operation
Documenter.jl copied to clipboard

ASCII colors not quite properly printed in LaTeX

Open mortenpi opened this issue 3 years ago • 6 comments

From the LaTeX showcase PDF:

image

mortenpi avatar Sep 20 '22 03:09 mortenpi

This seems tricky to do, because it looks like the formatting commands don't get written to the IO buffer:

(image to see the formatting)

image
julia> printstyled("Hello"; bold = true)
Hello
julia> printstyled(stdout, "Hello"; bold = true)
Hello
julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> printstyled(io, "Hello"; bold = true)

julia> String(take!(io))
"Hello"

odow avatar Sep 29 '22 20:09 odow

You need to do:

julia> io = IOBuffer()

julia> ioc = IOContext(io, :color=>true)

julia> printstyled(ioc, "Hello"; bold = true)

julia> String(take!(io))
"\e[0m\e[1mHello\e[22m"

KristofferC avatar Sep 29 '22 20:09 KristofferC

So then the follow-up question is: can the {minted} environment in LaTeX handle those control characters appropriately? I'd assume not, because the styling happens separately.

\begin{minted}{julia}
julia> io = IOBuffer()

julia> ioc = IOContext(io, :color=>true)

julia> printstyled(ioc, "Hello"; bold = true)

julia> String(take!(io))
"\e[0m\e[1mHello\e[22m"
\end{minted}

yields

image

odow avatar Sep 29 '22 20:09 odow

So then the follow-up question is: can the {minted} environment in LaTeX handle those control characters appropriately? I'd assume not, because the styling happens separately.

Would need to do the same as the HTML code does (parse it and render). See https://github.com/JuliaDocs/Documenter.jl/pull/1441, https://github.com/JuliaDocs/ANSIColoredPrinters.jl

I am not sure you can directly set colors for certain parts of a minted block. Perhaps to fix this perfectly one would need to render directly to latex code for code blocks without relying on any package.

KristofferC avatar Sep 29 '22 20:09 KristofferC

It would not be too difficult to add LaTexPrinter to ANSIColoredPrinters. latex (I never knew KaTeX supported the xcolor syntax before!)

One of the issues is terminal-dependent colors. For Documenter.LaTeX() (i.e. LaTeXWriter), ANSIColoredPrinters.LaTeXPrinter will have to mimic the internal behavior of minted.

Although this is not a Documenter.jl issue, I think we need to accept some restrictions on LaTeX objects embedded within HTML.

kimikage avatar Apr 07 '24 12:04 kimikage

\begin{minted}[escapeinside=@@]{jlcon}
julia> src = IOBuffer("Is \e[48;5;250mthis \e[38;5;198mword \e[mcolored?");
@@
julia> LaTeXPrinter(src)
@Is \colorbox[HTML]{bcbcbc}{this {\color[HTML]{ff0087}word }}colored?@
\end{minted}

latexpdf

escapeinside may be a possible technique. However, it is considered somewhat fragile. cf. https://github.com/gpoore/minted/blob/main/source/minted.pdf

kimikage avatar Apr 07 '24 14:04 kimikage