1

I am struggling with the use of conditionals, I have a basic example given below that will output the table included.

The code starts by defining \myletterA as the A character, and an array called \test as A,B,C. I have a function called \indenterer that I call inside my table loop below.

\documentclass[12pt]{article}
\usepackage[a6paper,landscape]{geometry}

\usepackage{arrayjobx}
\usepackage{ifthen}

\def\tand{&}

\begin{document}

\newcommand{\myletterA}{}
\newcommand{\myletterwriter}[1]{\renewcommand\myletterA{#1}}
    \myletterwriter{A}

\newarray\test
\newcommand{\indenterer}{%
    \readarray{test}{A&B&C}%writes A,B,C to array test
    \checktest(1)%reads the first value
    \cachedata%returns the value last read by \checktest
    \myletterA%returns the value stored i.e. A
    \ifx\cachedata\myletterA%
    a% \true case
    \else%
    b% false case
    \fi%
}

\newcounter{it}
\begin{tabular}{ll}%
\hline%
  \setcounter{it}{1}%
  \whiledo{\theit<4}{%
    {\indenterer} Y \tand z \\%
  \stepcounter{it}%
  }%
\end{tabular}

space
    \checktest(1)
    \cachedata
    \myletterA

\end{document}

The output lines of the table should display \cachedata\myletterA_ Y Z where the _ is either an a or b. So my question is why is this code always executing the false case (note the letter b instead of a) even though both \cashdata and \myletterA expand to give the same result?

enter image description here

  • Did you try \newcommand{\myletterwriter}[1]{\renewcommand*\myletterA{#1}}? – egreg Jan 19 '16 at 20:21
  • Inside the \indenterer, just before \ifx add \show\cachedata\show\myletterA and you'll see \cachedata is just a "macro", while \myletterA is a "long macro". – Werner Jan 19 '16 at 20:35
  • Thanks you two. I did not know about long vs not long, I'll have to look into it. – Bob Jan 19 '16 at 22:37
2

It's easy. ;-) With

\newcommand{\myletterwriter}[1]{\renewcommand\myletterA{#1}}

the macro \myletterA is \long, whereas \cachedata is defined with \def that makes it non \long.

Just do

\newcommand{\myletterwriter}[1]{\renewcommand*\myletterA{#1}}

enter image description here

  • Ah thanks egreg. I do have one more quick question, if I remove the {} surrounding \indenterer the code breaks with the error "Argument of \read@array has an extra }". Any idea why? – Bob Jan 19 '16 at 22:35
  • @Bob The problem are the & in the argument to \readarray that are mistaken for alignment points. One should plunge in the code of arrayjobx for seeing precisely why. On the other hand, I consider the package not more than a toy: there are far better ways for coping with arrays. – egreg Jan 19 '16 at 22:40
  • Why do you consider it such? What are the better ways to deal with arrays? – Bob Jan 19 '16 at 23:34
  • @Bob Look at tex.stackexchange.com/search?q=arrayjobx+user%3A4427 and some of the linked answers – egreg Jan 19 '16 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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