Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASCII colors not quite properly printed in LaTeX #1945

Open
mortenpi opened this issue Sep 20, 2022 · 6 comments
Open

ASCII colors not quite properly printed in LaTeX #1945

mortenpi opened this issue Sep 20, 2022 · 6 comments
Labels
Format: LaTeX Related to the LaTeX / PDF output help wanted Type: Bug

Comments

@mortenpi
Copy link
Member

mortenpi commented Sep 20, 2022

From the LaTeX showcase PDF:

image

@mortenpi mortenpi added Type: Bug help wanted Format: LaTeX Related to the LaTeX / PDF output labels Sep 20, 2022
@odow
Copy link
Collaborator

odow commented Sep 29, 2022

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"

@KristofferC
Copy link
Member

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"

@odow
Copy link
Collaborator

odow commented Sep 29, 2022

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

@KristofferC
Copy link
Member

KristofferC commented Sep 29, 2022

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 #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.

@kimikage
Copy link
Contributor

kimikage commented Apr 7, 2024

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
Copy link
Contributor

kimikage commented Apr 7, 2024

\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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Format: LaTeX Related to the LaTeX / PDF output help wanted Type: Bug
Projects
None yet
Development

No branches or pull requests

4 participants