latextikzpgfplots

Conditional selection of tikz style classes


I try to achieve that different style classes are chosen in a self-defined tikz function depending on the input number.

However, the \ifnum command doesn't seem to work as I expect it. The error message that I get is:

> thesis/image/outline_MWE.tex:46: Missing = inserted for \ifnum.
<to be read again> 
                   }
l.46    \makeoutlinefig{1}
                        
thesis/image/outline_MWE.tex:46: Missing number, treated as zero.
<to be read again> 
                   }
l.46    \makeoutlinefig{1}

MWE:

\documentclass[varwidth=true]{standalone}

    % \input{../preamble.tex}
    % \input{../colors.tex}

    \usepackage{tikz}
    \usetikzlibrary{shapes,arrows,arrows.meta, calc, decorations.markings, backgrounds,fit,positioning,plotmarks, intersections, patterns, intersections,decorations.text,external,decorations.pathreplacing}

\begin{document}

\DeclareRobustCommand{\makeoutlinefig}[1]{
        \centering
        \def\threebw{16.7cm}
        \begin{tikzpicture}     
            [auto, box/.style ={rectangle, 
                                % font= \tiny,
                                draw=black, 
                                thick, 
                                % fill=blue!30, 
                                text width=3.0cm,
                                minimum width=1.5cm,
                                align=center, 
                                rounded corners, 
                                minimum height=2.0cm, 
                                dashed, thick},
                    activebox/.style = {box, draw=red, 
                    thick,solid},       
                    node distance = 0.5cm,
                ]

            \node[style=\ifnum#1=1 activebox\else box\fi,
                        text width=\threebw] (b1) at (0,0) {\textbf{1. Chapter}};
            \node[style=\ifnum#1=2 activebox\else box\fi, 
                        text width=\threebw, below = of b1] (b2) {\textbf{2. Chapter}} ;

            \draw [-Stealth,ultra thick] (b1) -- (b2);
        \end{tikzpicture}
        }

    \makeoutlinefig{1}

    \end{document}


Solution

  • You can avoid the problem like this:

    \documentclass[varwidth=true]{standalone}
    
    \usepackage{tikz}
    \usetikzlibrary{shapes,arrows,arrows.meta, calc, decorations.markings, backgrounds,fit,positioning,plotmarks, intersections, patterns, intersections,decorations.text,external,decorations.pathreplacing}
    
    \begin{document}
    
    \DeclareRobustCommand{\makeoutlinefig}[1]{
    
      \centering
      \def\threebw{16.7cm}
      
      \begin{tikzpicture}[
        auto, 
        box/.style = {
          rectangle, 
          draw=black, 
          thick, 
          text width=3.0cm,
          minimum width=1.5cm,
          align=center, 
          rounded corners, 
          minimum height=2.0cm, 
          dashed, 
          thick
        },
        activebox/.style = {
          box, 
          draw=red, 
          thick,
          solid
        },       
        node distance = 0.5cm,
      ]
      
        \ifnum#1=1
          \def\mystyle{activebox}
        \else
          \def\mystyle{box}
        \fi
        
        \node[\mystyle, text width=\threebw] (b1) at (0,0) {\textbf{1. Chapter}};
        
        \ifnum#1=2
          \def\mystyle{activebox}
        \else
          \def\mystyle{box}
        \fi            
        
        \node[\mystyle, text width=\threebw, below = of b1] (b2) {\textbf{2. Chapter}} ;
        
        \draw [-Stealth,ultra thick] (b1) -- (b2);
      \end{tikzpicture}
      
    }
    
    \makeoutlinefig{2}
    
    \end{document}