# How can I use a conditional expression as key-value arguments?

I want to reduce the number of used keystrokes in my code. More precisely, I want to simplify

\ifodd\numexpr\ix+\iy\relax\psset{fillstyle=vlines,hatchsep=.5pt,hatchcolor=lightgray}\else\psset{fillstyle=solid}\fi
\psframe[origin={\ix,\iy}](-1,-1)


to

\psframe[origin={\ix,\iy},\ifodd\numexpr\ix+\iy\relax fillstyle=vlines,hatchsep=.5pt,hatchcolor=lightgray\else fillstyle=solid\fi](-1,-1)


Unfortunately, the latter does not compile with some errors that you can see by yourself.

Here is the complete code for both the former (that is the code to be simplified) and latter cases (that is the simplified code but failed).

## Unsimplified

\documentclass[pstricks,border=24pt]{standalone}
\usepackage{multido}
\begin{document}
\begin{pspicture}[dimen=m](8,8)
\multido{\ix=1+1}{8}
{
\uput[d](!\ix\space .5 sub 0){$\char\numexpr\ix+96\relax\mathstrut$}
\uput[l](!0 \ix\space .5 sub){$\ix\mathstrut$}
\multido{\iy=1+1}{8}
{
\ifodd\numexpr\ix+\iy\relax\psset{fillstyle=vlines,hatchsep=.5pt,hatchcolor=lightgray}\else\psset{fillstyle=solid}\fi
\psframe[origin={\ix,\iy}](-1,-1)
\rput(!\ix\space .5 sub \iy\space .5 sub){$(\ix,\iy)$}
}
}
\end{pspicture}
\end{document}


## Failed to be simplified

\documentclass[pstricks,border=24pt]{standalone}
\usepackage{multido}
\begin{document}
\begin{pspicture}[dimen=m](8,8)
\multido{\ix=1+1}{8}
{
\uput[d](!\ix\space .5 sub 0){$\char\numexpr\ix+96\relax\mathstrut$}
\uput[l](!0 \ix\space .5 sub){$\ix\mathstrut$}
\multido{\iy=1+1}{8}
{
\psframe[origin={\ix,\iy},\ifodd\numexpr\ix+\iy\relax fillstyle=vlines,hatchsep=.5pt,hatchcolor=lightgray\else fillstyle=solid\fi](-1,-1)
\rput(!\ix\space .5 sub \iy\space .5 sub){$(\ix,\iy)$}
}
}
\end{pspicture}
\end{document}


How to solve this issue?

-

You want a conditional value for the value

    \psframe[origin={\ix,\iy},
fillstyle=\ifodd\numexpr\ix+\iy\relax vlines\else solid\fi,
hatchsep=.5pt,
hatchcolor=lightgray](-1,-1)


which is OK here as the hatchxxx keys are ignored in the solid case.

Or you need a call to \psframe with a different number of arguments, either as in your question or the other answer.

-

The problem lies that mactro \psframe must be expanded after \ifodd and latex normally does sequential expansion. The order of expansion can be changed using number of \expandafter macros or using for example \edef combined with \noexpand:

\documentclass[pstricks,border=24pt]{standalone}
\usepackage{multido}
\begin{document}
\begin{pspicture}[dimen=m](8,8)
\multido{\ix=1+1}{8}
{
\uput[d](!\ix\space .5 sub 0){$\char\numexpr\ix+96\relax\mathstrut$}
\uput[l](!0 \ix\space .5 sub){$\ix\mathstrut$}
\multido{\iy=1+1}{8}
{
\edef\my{\noexpand\psframe[origin={\ix,\iy},\ifodd\numexpr\ix+\iy\relax fillstyle=vlines,hatchsep=.5pt,hatchcolor=lightgray\else fillstyle=solid\fi](-1,-1)}
\my
\rput(!\ix\space .5 sub \iy\space .5 sub){$(\ix,\iy)$}
}
}
\end{pspicture}
\end{document}

-
Welcome to TeX.SX! –  Andrew Swann Apr 6 at 9:21