projekter/yquant

Inconsistent results with beamer overlays

Closed this issue · 3 comments

Describe the bug
When I introduce overlay commands in yquant, the output is inconsistent.

To Reproduce

\documentclass{beamer}
\usepackage{tikz}
\usepackage[compat=0.4]{yquant}
\begin{document}
\begin{frame}
  \begin{tikzpicture}
    \begin{yquant}
    qubit q[2];
    \only<2>{[fill=red]}
    h q[0];
    cnot q[1] | q[0];
  \end{yquant}
\end{tikzpicture}
\end{frame}
\end{document}

Expected output
For this specific example, I expect the H gate to have a red color on the second slide.

Screenshots
Output of previous code
image

Environment

  • TeX distribution:
$>tex --version
TeX 3.141592653 (TeX Live 2021/Arch Linux)
  • Compiler (pdf, Lua, ...): pdflatex
  • Version of all formats and packages
info yquant
yquant:
  Category: Package
  ShortDesc: Typesetting quantum circuits in a human-readable language
  LongDesc: This LaTeX package allows to quickly draw quantum circuits. It bridges the gap between the two groups of packages that already exist: those that use a logic-oriented custom language, which is then translated into TeX by means of an external program; and the pure TeX versions that mainly provide some macros to allow for an easier input. yquant is a pure-LaTeX solution -- i.e., it requires no external program -- that introduces a logic oriented language and thus brings the best of both worlds together. It builds on and interacts with TikZ, which brings an enourmous flexibility for customization of individual circuit.
  Size: 50676
  Collection: texlive-science
  Revision: 60420
  Installed: Yes (as texlive-local-yquant version 58712)

I would say that the main issue here is not the missing highlighting, but the missing anything (there are no gates at all). This is due to the fact that you mix TeX commands (\only) with yquant commands. While this is in general possible, you must be careful to restart the yquant parser after every set of macros (saying \yquant).
Background: Due to the nature of TeX macros and expansion, it is not possible to do this automatically for any arbitrary macro. However, it is possible for very specific macros where we know the exact definition - this is the reason why yquant will automatically take care of this whenever you use TikZ macros (such as \draw, \fill, ...). But this automatism is not implemented for beamer macros - though I think this might be a very meaningful addition.

While I would definitely consider implementing it, the situation that you gave is probably also the best showcase of how such an automatism would probably fail. The attributes of a yquant gate command and the gate itself belong together and cannot be split; however, this is precisely what you do here. So if I were to implement an automatism, you would have to write the gate commands twice - once with and once without attributes. Without this automatism, you can actually hack it together to work:

    qubit q[2];
    \alt<2>{\yquant [fill=red]}{\yquant}
    h q[0];
    cnot q[1] | q[0];

Here, I restart the yquant parser both after the execution of the \only part (and due to the way of how \alt expands, the second braced content will be completely removed from the input stream, so it is actually not present on the second slide) and also after all the other slides.
I have to check all the beamer macros whether there is a meaningful way to construct a workaround such as in this case, as the naive way to map \only<x>{y} to something as \only<x>{\yquant y}\yquant, will completely break things in this case.

Thanks again @projekter, I confirm it works like a charm. I am wondering if this mechanism would work also for "undiscovering" layers of the circuit (i.e., layers of gates) once at a time, but maybe this will be the object of another question in the future.

I do not have such a good knowledge of tex/beamer macros to provide an adequate reply on that. I let you decide if to close the issue or to keep it open to have a reference for your implementation of the workaround.

The current master contains new updates that should make the integration with beamer much more easy. In particular, what you wrote in your first post should now work as expected without all the parser issues. I created patches for \only, \alt, \temporal, \uncover, \visible, \invisible, and \note (well, these are non-intrusive patches: I don't change the macros, but if the yquant parser recognizes one of those macros, it will take special care.
Also \onslide and \pause should in principle work with simple circuits, although there are no guarantees for more complex ones (as also states in the beamer manual itself, so I'm fine with this).
The beamer integration is documented in the new section 8.2. I would appreciate some testing in real-live use cases before this goes to the CTAN.