4
\documentclass{article}

\usepackage{xeCJK}

\usepackage{xcolor}
\colorlet{color1}{blue!50!black}
\colorlet{color2}{green!50!black}

\usepackage{etoolbox}
\newtoggle{col}

\providecommand{\tog}%
    {%
    \iftoggle{col}%
        {\color{color1}\togglefalse{col}}%
        {\color{color2}\toggletrue{col}}%
    }%
    
\usepackage{paracol}

\maxdeadcycles=10000

\begin{document}

\begin{paracol}{1}

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z


\end{paracol}

\end{document}

I'm writing a document in Chinese, and I'm using XeTeX to compile and get Chinese fonts. I'm also using paracol to add marginal notes. I've written a function \tog to toggle the font color, for the purpose of highlighting Chinese word boundaries. To facilitate editing, I want to use a command that toggles between two colors, and not explicitly state the colors (otherwise if I delete a word, then I need to change the colors in the whole document).

I had to first overcome this problem by adding in \maxdeadcycles=10000, but even then it basically just gives up after a while:

the alphabet written repeatedly; it initially alternates between green and blue, but gives up after a while and everything is blue

There must be a better way. I just want to toggle the font color whenever there is a \tog (which I'll need to call thousands of times); it shouldn't be so hard.

Question: Is there a (better) way to repeatedly toggle between two font colors within a paracol environment, and compiling with XeTeX?

3
  • Unrelated: I compile OK without the \maxdeadcycles=10000 line. Unrelated 2: to save typing, you could define a 1-character toggler, like\newcommand{\北}% {% \iftoggle{col}%....
    – Cicada
    Jun 26 at 10:40
  • Your loop works well with and without setting \maxdeadcycles with pdflatex and lualatex with the default font, so the problem is that xeCJK need xelatex but xelatex fail for some reason. I do not have idea of Chinese, but if you can write it without xeCJK and xelatex, this could be a solution.
    – Fran
    Jun 26 at 11:12
  • Looks like xelatex has a limit of about 64 colour changes. (PDF specials staking up, maybe?? don't know; wild guess)
    – Cicada
    Jun 26 at 12:20

2 Answers 2

7

This bug is happening because that many color changes in the same environment is exceeding the capacity of the XeTeX color stack. Your bug is a literal stack overflow.

One way to solve it in XeTeX is to make each color change inside a \textcolor command, so that each color change is made for a short time, then ends before the next is made. XeTeX does not then try to remember all the color changes until the end of the environment so that it could undo them all if it needed to, which gets it all confuddled.

\documentclass{article}

\usepackage{xeCJK}

\usepackage{xcolor}
\colorlet{color1}{blue!50!black}
\colorlet{color2}{green!50!black}

\usepackage{etoolbox}
\newtoggle{col}

\providecommand{\tog}[1]%
    {%
    \iftoggle{col}%
        {\textcolor{color1}{#1}\togglefalse{col}}%
        {\textcolor{color2}{#1}\toggletrue{col}}%
    }%
    
\usepackage{paracol}

\maxdeadcycles=10000

\begin{document}

\begin{paracol}{1}

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z


\end{paracol}

\end{document}

Text Sample

Although you’d probably be writing something more like \tog{uvw}\tog{xy}. I just didn’t rewrite that whole MWE when I didn’t need to.

Also, if your color changes are really that regular, you could be doing them inside a loop.

1
  • So, pdflatex and lualatex don't have this limit? or the limit is larger in these two?
    – Tom
    Jun 26 at 19:10
3

@Fran's idea is a good one.

Based on How to use Chinese with lualatex? , luatexja is an option.

colours

[Text - Wikipedia - Solar System]

MWE

\documentclass{article}

%\usepackage{xeCJK}
\usepackage{luatexja-fontspec}
\setmainjfont{FandolSong}

\usepackage{xcolor}
\colorlet{color1}{blue}
\colorlet{color2}{red}

\usepackage{etoolbox}
\newtoggle{col}

\newcommand{\tog}%
    {%
    \iftoggle{col}%
        {\color{color1}\togglefalse{col}}%
        {\color{color2}\toggletrue{col}}%
    }%

%to save typing:
\newcommand{\北}{\tog}
\catcode`\⛯=\active %arbitrary symbol as shortcut
\def⛯{\北}
    
\usepackage{paracol}


\begin{document}

\begin{paracol}{1}

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z

\tog a\tog b\tog c\tog d\tog e\tog f\tog g\tog h\tog i\tog j\tog k\tog l\tog m\tog n\tog o\tog p\tog q\tog r\tog s\tog t\tog u\tog v\tog w\tog x\tog y\tog z


⛯太陽系⛯是⛯一个⛯受太⛯阳引力约⛯束在⛯一起⛯的⛯行星系统

\北 太陽系\北 是\北 一个⛯受太⛯阳引力约⛯束在⛯一起⛯的⛯行星系统,包括⛯太⛯阳⛯以⛯及⛯直⛯接⛯或⛯间⛯接⛯围⛯绕⛯太⛯阳⛯运⛯动⛯的⛯天⛯体[a]。etc    ... 在直接围绕太阳运动的天体中,最大的八颗被称为行星[b],其余的天体要比行星小很多,比如矮行星、太阳系小行星和彗星。軌道間接围绕太陽运动的天體是衛星,其中有兩顆比最小的行星水星還要大[c]。 



\end{paracol}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.