tecosaur / org-latex-preview-todos

A tracker for the Org LaTeX Preview effort on https://git.tecosaur.net/tec/org-mode
4 stars 0 forks source link

Tikz does not respect color specified earlier in tex document #6

Open karthink opened 6 months ago

karthink commented 6 months ago

When the color is specified earlier in the tex document (like with \color[rgb]{0,0,0.00392157}), Tikz and circuitikz elements that follow don't respect it. This causes color issues with the LaTeX preview. This file has an example:

% Intended LaTeX compiler: pdflatex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
% Package hyperref omitted
\usepackage{amssymb}
\usepackage{amsmath}
% Package cleveref omitted
\usepackage{cancel}
% Package color omitted
% Package booktabs omitted
% Package longtable omitted
% Package xcolor omitted
% Package scrextend omitted
\usepackage{xcolor}

\usepackage{circuitikz}

% Generated preamble omitted for snippets.

\makeatletter
\renewcommand{\theequation}{\(\diamond\)\ifnum\value{equation}>1%
\,+\,\@arabic{\numexpr\value{equation}-1\relax}\fi}
\makeatother
\usepackage[active,tightpage,auctex,dvips]{preview}

\setlength{\textwidth}{0.6\paperwidth}

\begin{document}

\setlength\abovedisplayskip{0pt} % Remove padding before equation environments.

\begin{preview}
\color[rgb]{0,0,0.00392157}\setcounter{equation}{0}%
\( x + 3 \)
\end{preview}

\begin{preview}
\setcounter{equation}{0}%
\begin{circuitikz}
\draw (0,0) to[R, i=2] (5,0);
\end{circuitikz}

\end{preview}

\begin{preview}
\setcounter{equation}{0}%
\begin{center}
\begin{circuitikz}[american voltages]
\draw
  (0,0) to [short, *-] (6,0)
  to [V, l_=$\mathrm{l}{\omega}_m \underline{\psi}^s_R$] (6,2)
  to [R, l_=$R_R$] (6,4) 
  to [short, i_=$\underline{i}^s_R$] (5,4) 
  (0,0) to [open, v^>=$\underline{u}^s_s$] (0,4) 
  to [short, *- ,i=$\underline{i}^s_s$] (1,4) 
  to [R, l=$R_s$] (3,4)
  to [L, l=$L_{\sigma}$] (5,4) 
  to [short, i_=$\underline{i}^s_M$] (5,3) 
  to [L, l_=$L_M$] (5,0); 
\end{circuitikz}
\end{center}

\end{preview}

\end{document}

A simple workaround would be to specify the color for each fragment, but this will break dvipng conversion processes unexpectedly since it cannot handle more than 99 \color specials in the DVI.

karthink commented 6 months ago

This is solved with the latest updates but only for dvisvgm 3.1+ (with currentColor support). It's difficult to solve for older dvisvgm versions without some extra code. I think we can simply mention this as a known issue and recommend upgrading to dvisvgm 3.1.

tecosaur commented 6 months ago

That seems like the most appropriate thing to do at this stage.

karthink commented 6 months ago

Where do we place this advisory? The manual?

tecosaur commented 6 months ago

That's probably the best place

karthink commented 6 months ago

Okay, let's keep this issue open until we add it to the manual.

yantar92 commented 6 months ago

karthink @.***> writes:

Where do we place this advisory? The manual?

It might also be helpful to produce a warning when the installed dvisvgm version is too old or when dvisvgm is not installed. This way, users can see what is the problem without digging.

-- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at https://orgmode.org/. Support Org development at https://liberapay.com/org-mode, or support my work at https://liberapay.com/yantar92

karthink commented 6 months ago

It might also be helpful to produce a warning when the installed dvisvgm version is too old or when dvisvgm is not installed. This way, users can see what is the problem without digging.

dvisvgm 3.1 is new, it's not part of any texlive release yet. Most users will end up seeing this warning with no way to fix it (beyond silencing the warning itself). The coloring error is limited to tikz previews, dvisvgm works fine otherwise, so I think we can wait a couple of years before issuing a warning when we see dvisvgm < 3.1.

yantar92 commented 6 months ago

karthink @.***> writes:

It might also be helpful to produce a warning when the installed dvisvgm version is too old or when dvisvgm is not installed. This way, users can see what is the problem without digging.

dvisvgm 3.1 is new, it's not part of any texlive release yet. Most users will end up seeing this warning with no way to fix it (beyond silencing the warning itself). The coloring error is limited to tikz previews, dvisvgm works fine otherwise, so I think we can wait a couple of years before issuing a warning when we see dvisvgm < 3.1.

If there is no catastrophic breakage with older dvisvgm, I agree.

-- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at https://orgmode.org/. Support Org development at https://liberapay.com/org-mode, or support my work at https://liberapay.com/yantar92

tecosaur commented 5 months ago

If there is no catastrophic breakage with older dvisvgm, I agree.

The changes we're making to take advantage of newer dvisvgm versions are fully backwards-compatible. Newer is better, but we'll work with whatever's installed.

karthink commented 5 months ago

Unfortunately dvisvgm 3.1+ does not fix the issue of tikz not respecting the specified foreground color.