8

I would expect that using the \MyText macro defined as

\newcommand{\MyText}{before \textcolor{red}{XX} after}

should always yield the same color for the text "before" and "after". But it appears that when I use it within a \node (third line), it results in different colors:

enter image description here

It seems as if the \textcolor{red}{XX} restores the color to black at the end if it is used within a \node.

Question: How do I get \textcolor{}{} within a \node to yield the same results as it does outside of a \node.

Listfiles

 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
    tikz.sty    2015/08/07 v3.0.1a (rcs-revision 1.151)
     pgf.sty    2015/08/07 v3.0.1a (rcs-revision 1.15)
  pgfrcs.sty    2015/08/07 v3.0.1a (rcs-revision 1.31)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.1a (rcs-revision 1.7)
graphicx.sty    2017/06/01 v1.1a Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2017/06/25 v1.2c Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  pdftex.def    2018/01/08 v1.0l Graphics/color driver for pdftex
  pgfsys.sty    2014/07/09 v3.0.1a (rcs-revision 1.48)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.1a (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.1a (rcs-revision 1.1)
  pgffor.sty    2013/12/13 v3.0.1a (rcs-revision 1.25)
 pgfkeys.sty    
 pgfkeys.code.tex
 pgfmath.sty    
 pgfmath.code.tex
  pgffor.code.tex
    tikz.code.tex
supp-pdf.mkii
epstopdf-base.sty    2016/05/15 v2.6 Base part for package epstopdf
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
  grfext.sty    2016/05/16 v1.2 Manage graphics extensions (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
kvoptions.sty    2016/05/16 v3.12 Key value format for package options (HO)
kvsetkeys.sty    2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty    2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
pdftexcmds.sty    2018/01/30 v0.27 Utility functions of pdfTeX for LuaTeX (HO)
   ifpdf.sty    2017/03/15 v3.2 Provides the ifpdf switch
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live

Code:

\documentclass{article}\listfiles
\usepackage{tikz}

\newcommand{\MyText}{before \textcolor{red}{XX} after}

\begin{document}
\noindent
Outside of \verb|\node|: \MyText

\begingroup
\noindent
Outside of \verb|\node|: \color{green}\MyText
\endgroup

\noindent
\begin{tikzpicture}%
    \node [text=green, draw=blue, fill=yellow!20, inner sep=2pt] at (0,0) {Inside node: \MyText};
\end{tikzpicture}%
\end{document}
Peter Grill
  • 223,288
  • 2
    That's really curious. (+1) Interestingly, if you use green instead of text=green, i.e. \node [green, inner sep=0pt] at (0,0) {Inside node: \MyText}; you get the expected or standard result. –  Aug 24 '18 at 18:08
  • @marmot: Hmm. Good observation. Took me a long time to narrow down the problem to this so didn’t think of trying that. But, I need different \draw color so that won’t work for me. – Peter Grill Aug 24 '18 at 18:18
  • I was hoping you could append that information to your nice question such that it will be easier to find out what's going on. (Some long time I looked into the tikz code and indeed the text color gets treated differently from the other colors, in particular it is the only color that gets stored in a variable from "outside", the name of the macro being \tikz@textcolor. This might suggest that this macro gets reseted.) So perhaps this information is useful for someone who has more experience and also time to look into this. –  Aug 24 '18 at 18:24
  • 1
    BTW, \node [green,draw=red, inner sep=0pt] at (0,0) {Inside node: \MyText}; also gives the expected result... –  Aug 24 '18 at 18:29
  • 3
    Related: https://tex.stackexchange.com/q/398675/43317: Maybe you can use font=\color{green} – esdd Aug 24 '18 at 18:36
  • 2
    @marmot: Updated question to show different \draw (and \fill) colors. Using just the color without the text= might be a good workaround for now. Only issue with that then is that the options must be specified in order. – Peter Grill Aug 24 '18 at 18:37
  • @esdd: Yeah that was what I was doing initially, but that was leading other problems (in my code, not the MWE) and then after I realized that that was kind of a hack, decided not to pursue that further. – Peter Grill Aug 24 '18 at 18:39
  • Perhaps also related. At least that's how I know I may abuse \tikz@textcolor. And yes, the order is critical. To me this seems due to a little bit strange handling in \tikzoption{color}{% in /usr/local/texlive/2018/texmf-dist/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex. –  Aug 24 '18 at 18:40
  • 2
    The important thing to note here is it's not my fault – David Carlisle Aug 24 '18 at 19:09
  • 1
    @DavidCarlisle: :-) When I finally found that \textcolor was the culprit I immediately thought "no, it can't possibly David's fault"... until I realized it had to be in a \node to reproduce the problem. Boy was I releived! – Peter Grill Aug 24 '18 at 21:50
  • 1
    @marmot: \color{.} should work, if it is known what . is at the time. I think that was my exact issue with using \font=\color{} instead of \text={}. – Peter Grill Aug 24 '18 at 22:10
  • 1
    Related, probably: https://tex.stackexchange.com/questions/300423/how-to-apply-colour-options-to-the-contents-of-tikz-nodes-when-the-content-inclu. @marmot Originally a Forest question :-). – cfr Aug 25 '18 at 03:53

1 Answers1

2

Here is the hack I have come up with as a temporary workaround:

\makeatletter
\let\OldTextcolor\textcolor
\newcommand*{\MyTextColor}[2]{%
    \colorlet{@SavedColor}{.}%
    \OldTextcolor{#1}{#2}%
    \color{@SavedColor}%
    \xspace%
}
\makeatother
\AtBeginEnvironment{tikzpicture}{\let\textcolor\MyTextColor}%

which yields (in this test case):

enter image description here

Note:

Code:

\documentclass{article}\listfiles
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{xspace}
\usepackage{tikz}

\newcommand{\MyText}{before \textcolor{red}{XX} after}

\makeatletter \let\OldTextcolor\textcolor \newcommand*{\MyTextColor}[2]{% \colorlet{@SavedColor}{.}% \OldTextcolor{#1}{#2}% \color{@SavedColor}% \xspace% } \makeatother \AtBeginEnvironment{tikzpicture}{\let\textcolor\MyTextColor}%

\begin{document} \noindent Outside of \verb|\node|: \MyText

\begingroup \noindent Outside of \verb|\node|: \color{green}\MyText \endgroup

\noindent \begin{tikzpicture}% \node [text=green, draw=blue, fill=yellow!20, inner sep=2pt] at (0,0) {Inside node: \MyText}; \end{tikzpicture}% \end{document}

Peter Grill
  • 223,288