Tikz node color resets

colornodestikz-pgf

I am having trouble with node colors resetting after specifying a color for a single word. I have the following MWE where I have the string 3\\ 4\\ 5\\ 6 in which I have specified that 4 be \red{} and I need the other remaining words to be a secondary color, in this example I have chosen blue. Please note that for the purpose of this question the string definition should not be altered in any way. I read similar questions about a bug in tikz or xcolor from a few years ago related to boxes, but I am unsure if this issue is related or not.

the 5 and 6 should also be colored blue

\documentclass[10pt]{standalone}

\usepackage{xparse}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{tikz}
    \usetikzlibrary{arrows,automata,positioning,calc}


\NewDocumentCommand{\size}{m}{%
    \fontsize{#1}{#1}\selectfont%
}%
\NewExpandableDocumentCommand{\red}{m}{%
    {\color{red}#1}%
}%
\NewExpandableDocumentCommand{\mystring}{}{%
    3\\ \red{4}\\ 5\\ 6%
}%


\begin{document}

\begin{tikzpicture}[x=1in,y=1in]
    \node[draw, align=center, text=blue, font=\bfseries\size{80}] at (0,0) {\mystring};
\end{tikzpicture}

\begin{tikzpicture}[x=1in,y=1in]
    \node[draw, align=center, font=\bfseries\size{80}] at (0,0) {\color{blue} \mystring};
\end{tikzpicture}

\end{document}

Edit: Observation, It should be noted that if the color red is not specified, the first column will all be blue, where in the second test only the first letter is blue.

Best Answer

Could you consider using a matrix? This way you can fix the color for every row with independence of the matrix general color.

The syntax for \mystring keeps \\ between rows. You can select the color for a row with a row x/.style on the matrix options or fix it inside the list or rows like in \mystringwithcolorsinside.

A TiKZ matrix finishes with \\. I've added it after \mystring command, but you could insert them as last list characters.

\documentclass[10pt]{standalone}

\usepackage{xparse}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{tikz}
    \usetikzlibrary{arrows,automata,positioning,calc,matrix}


\NewDocumentCommand{\size}{m}{%
    \fontsize{#1}{#1}\selectfont%
}%
\NewExpandableDocumentCommand{\red}{m}{%
    {\color{red}#1}%
}%
\NewExpandableDocumentCommand{\mystring}{}{%
    3\\ 4\\ 5\\ 6%
}%

\NewExpandableDocumentCommand{\mystringiwthcolorsinside}{}{%
    3\\|[text=green]| 4\\ 5\\ 6%
}%

\tikzset{mycolumn/.style={matrix, matrix of nodes, draw, inner sep=0pt, 
    text=blue, font=\bfseries\size{80}, nodes={align=center, inner sep=.3333em},
    row 2/.style={text=red}}}
    
\begin{document}

\begin{tikzpicture}[x=1in,y=1in]
    \node[mycolumn] at (0,0) {\mystring\\};
\end{tikzpicture}

\begin{tikzpicture}[x=1in,y=1in]
    \node[mycolumn] at (0,0) {\mystringiwthcolorsinside\\};
\end{tikzpicture}

\end{document}

enter image description here