2

I would like to do something like:

enter image description here

using PGF styles in TikZ

Here is the code:

\documentclass[tikz,convert={outfile=mwe.png}]{standalone}
\begin{document}
\pgfkeys{%
  /tikz/style A/.style = {text=blue},
  /tikz/style B/.style = {text=red},
}

\begin{tikzpicture}
  \node [thin, draw, style A] at (1,3) {Style A};
  \node [thin, draw, style B] at (1,2) {Style B};
  \node [thin, draw] at (1,1) {Style A + Style B};
\end{tikzpicture}
\end{document}

But the "Style A + Style B" is always the same color. How can I do to get "Style A" in style A and "Style B" in style b and the "+" in the node style?

Of course I could use something like {\color{blue}Style A} + {\color{red}Style B} but it's not very flexible. I prefer to use styles instead.

Thanks in advance.

renard
  • 742
  • 5
  • 15
  • So, do you want it so that only "Style A" uses style A? As in, a defined set of words is written like that? Or any text? Because the solutions might change. – Alenanno Aug 04 '16 at 11:08
  • If it is possible for any text I prefer that solution. – renard Aug 04 '16 at 11:13
  • 1
    Then automatic becomes more difficult. How would you distinguish what text is colored with which color? There needs to be a rule for Latex to use, it cannot read your mind. :D – Alenanno Aug 04 '16 at 11:17
  • I was wondering how I can write something like: \node [thin, draw] at (1,1) {\tikzusestyle[style a]{Style A} + \tikzusestyle[style a]{Style B}}; or something similar. – renard Aug 04 '16 at 11:35

2 Answers2

2

If you want this to work for any text then I can see two options which allow you to keep the flexability in that you only need to change the color once.

The first method uses \colorlet to define the colors to use and then applies the solution you proposed via \textcolor.

\colorlet{StyleAColor}{blue}
\colorlet{StyleBColor}{red}

\tikzset{ style A/.style={text=StyleAColor}, style B/.style={text=StyleBColor}, }

\node [thin, draw] at (1,1) {\textcolor{StyleAColor}{Style A} + \textcolor{StyleBColor}{Style B}};

The second option involves nesting tikzpictures and this is not recommended:

\node [thin, draw] at (1,0) {
    {\tikz[baseline=0pt] \node[style A, Inner Style] {Style A};} 
    {\tikz[baseline=0pt] \node[Inner Style] {+};}
    {\tikz[baseline=0pt] \node[style B, Inner Style] {Style B};}

enter image description here

Code:

\documentclass[border=2pt]{standalone}

\usepackage{tikz} \usepackage{xcolor}

\colorlet{StyleAColor}{blue} \colorlet{StyleBColor}{red}

\tikzset{ Inner Style/.style={inner sep=0pt, outer sep=0pt}, style A/.style={text=StyleAColor}, style B/.style={text=StyleBColor}, }

\begin{document} \begin{tikzpicture} \node [thin, draw, style A] at (1,3) {Style A}; \node [thin, draw, style B] at (1,2) {Style B}; \node [thin, draw] at (1,1) {\textcolor{StyleAColor}{Style A} + \textcolor{StyleBColor}{Style B}}; \node [thin, draw] at (1,0) { {\tikz[baseline=0pt] \node[style A, Inner Style] {Style A};} {\tikz[baseline=0pt] \node[Inner Style] {+};} {\tikz[baseline=0pt] \node[style B, Inner Style] {Style B};} }; \end{tikzpicture} \end{document}

Peter Grill
  • 223,288
0

Based on @peter-grill's answer (thanks to him) I came up with that working solution that work for my needs:

\documentclass[tikz,convert={outfile=mwe.png}]{standalone}
\begin{document}
\pgfkeys{%
  /tikz/Inner Style/.style={ultra thin, draw},
  /tikz/style A/.style = {text=blue},
  /tikz/style B/.style = {text=red},
  /tikz/nested/.style = {%
    baseline=(current bounding box.base),
    every node/.append style={inner sep=0pt, outer sep=0pt, anchor=base, Inner Style},
  },
}

\begin{tikzpicture}
  \node [thin, draw, nested] at (1,-1) {%
    {\tikz \node[style A]{e};}%
    {\tikz \node[]{epe};}%
    {\tikz \node[style B]{e};}
  };
  \node [thin, draw, nested] at (1,0) {%
    {\tikz \node[style A]{Style a};}%
    {\tikz \node[]{+};}%
    {\tikz \node[style B]{Style b};}
  };

\end{tikzpicture}

\end{document}

enter image description here

Note: The trick is to play with the baseline and anchor options.

renard
  • 742
  • 5
  • 15