# Using conditionals in tikzset

EDIT Through carelessness, I misrepresented the error that I was getting. I have corrected the example code and my comments.

I'd like to be able to setup 1 TikZ node .style altered by conditionals. The advantage is having one .style instead of many. Simple conditionals work, but anything requiring \ifsometest something=something else \fi fails.

MWE

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{lipsum}

\newif\ifdash
\newif\ifdouble

\def\doublecolor{yellow}
\def\mydots{dash dot dot}

\tikzset{%
boxes/.style={%
thick,
draw,
% This works:
\ifdash \mydots\fi,
% This works:
% double=\doublecolor,
% But this causes error: Use of \pgfkeys@sp@b doesn't match its definition.
\ifdouble double=\doublecolor \fi,
% As does this:
% \ifdouble double/.expand once=\doublecolor\fi,
}%
}

\begin{document}

\tikz\node[boxes] {\begin{minipage}{4in}\lipsum[1]\end{minipage}};

\dashtrue

\tikz\node[boxes] {\begin{minipage}{4in}\lipsum[1]\end{minipage}};

\end{document}


This fails with this error message:

./if-in-node-options.tex:30: Use of \pgfkeys@sp@b doesn't match its definition.

\pgfkeys@spdef ...e \pgfkeys@sp@a #2\pgfkeys@stop
\pgfkeys@stop        \pgfkeys@st...
l.30    \tikz\node[boxes]
{\begin{minipage}{4in}\lipsum[1]\end{minipage}};


The question is this: Is there any way to do something like:

\ifdouble double=\doublecolor\fi


or not? Again, while multiple styles are possible based on the requirements -- which I've already done -- I'm looking for something simpler if possible.

• The r is missing from \doublecolor in your MWE, is that on purpose? – Marijn Apr 7 '18 at 21:11
• No ;( , not on purpose -- an annoying finger-fluff. Corrected. Thanks. – sgmoye Apr 7 '18 at 21:14

I recommend the following:

\tikzset{%
boxes/.code={
\pgfkeysalso{
thick,
draw
}
\ifdouble
\pgfkeysalso{double=\doublecolor}
\fi
}%
}


some key/.style={some other keys,...} is shorthand for some key/.code={\pgfkeysalso{some other keys,...}}. If you need any logic in your style code, one solution is to fall back to using the /.code handler and \pgfkeysalso around the actual keys.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{lipsum}

\newif\ifdash
\newif\ifdouble

\def\doublecolor{yellow}
\def\mydots{dash dot dot}

\tikzset{%
boxes/.code={
\pgfkeysalso{
thick,
draw
}
\ifdouble
\pgfkeysalso{double=\doublecolor}
\fi
}%
}

\begin{document}

\tikz\node[boxes] {\begin{minipage}{4in}\lipsum[1]\end{minipage}};

\dashtrue

\tikz\node[boxes] {\begin{minipage}{4in}\lipsum[1]\end{minipage}};

\end{document}


Two problems with the approach of the question:

• The syntax characters = and , should not be inside an \if construct.

• The option settings in \tikzset is executed immediately. Setting the \if switches later has no effect.

The following example uses styles to add additional properties to style boxes.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{lipsum}

\def\doublecolor{yellow}
\def\mydots{dash dot dot}

\tikzset{%
boxes/.style={%
thick,
draw,
},
set dash/.style={%
boxes/.append style={\mydots},%
},%
set double/.style={%
boxes/.append style={double=\doublecolor},%
},%
}

\begin{document}

\tikz\node[boxes] {\begin{minipage}{4in}\lipsum[2]\end{minipage}};

\medskip
\tikz[set dash, set double]
\node[boxes] {\begin{minipage}{4in}\lipsum[2]\end{minipage}};

\end{document}