Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this 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}
share|improve this answer
    
Welcome to TeX.SX! –  Andrew Swann Apr 6 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.