0

I want to draw a huge QCircuit diagram recursively. So I want a recursive macro like this:

\documentclass{article}
\usepackage{qcircuit}

\newcommand\recurse[1]{
\ifnum1=#1
    \gate{Y}
\else
    \gate{X} & \recurse{\numexpr#1-1}
\fi
}

\begin{document}

\Qcircuit @C=1em @R=0.7em {
& \recurse{3}
}
\end{document}

This gives me several errors, all for Line 16:

Missing number, treated as zero.<to be read again> < }
Xy-pic error: in entry "1,5":No [0,-1] (is "1,4") from here....
Incomplete ifnum; all text was ignored after line 16.<inserted text>\fi
Missing } inserted.<inserted text>} }

However, the following code compiles perfectly and does exactly what's expected:

\documentclass{article}
\usepackage{qcircuit}

\newcommand\recurse[1]{
\ifnum1=#1
    $Y$
\else
    $X$, \recurse{\numexpr#1-1}
\fi
}

\begin{document}

\recurse{3}
\end{document}

I think Qcircuit might be somehow trying to build the table for the circuit before the macro has finished inputting all the text, and so the constructed table is too small. Is there any way to fix this? Or should I just give up on using macros to do this?

1

Rearranging the recursion a bit:

enter image description here

\documentclass{article}
\usepackage{qcircuit}


\newcommand\recurse[2]{%%%
\ifnum1=#1\relax
    \afterelse
    #2&\gate{Y}%
\else
   \afterfi
   \recurse{\numexpr#1-1}{#2&\gate{X}}%
\fi}

\def\afterelse#1\else#2\fi{\fi#1}
\def\afterfi#1\fi{\fi#1}

\begin{document}

\Qcircuit @C=1em @R=0.7em {
\recurse{3}{}
}
\end{document}

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.