I am trying to make an exercise sheet that should contain the solutions in the LaTeX code as well. Using package options I can select whether the solutions should be typeset or not. I got a problem and extracted a MWE:
\documentclass{scrartcl}
\usepackage{environ}
% \NewEnviron{Loesung}{\BODY}
\newenvironment{Loesung}{}{}
\usepackage{tikz}
\usetikzlibrary{circuits.logic.IEC}
\tikzset{>=stealth,circuit logic IEC}
\begin{document}
Task: Draw a NAND and a NOR gate.
\begin{Loesung}
\begin{tikzpicture}
\matrix[column sep=7mm]%,ampersand replacement=&]%
{
\coordinate (A); \
& \node [nand gate] (g1) {} ; & \node [nor gate] (g2) {}; & \coordinate (out); \
\coordinate (B); \
};
\end{tikzpicture}
\end{Loesung}
\end{document}
If I use the environment like in the code above, it compiles all right. As soon as I switch to the alternative using \NewEnviron, there are quite some errors from the TikZ drawing.
You can replace the ampersands with other macros (here I prepared for \&). You can replace the ampersands in the drawing and it should work even with environ created environments.
My idea was to either conditionally define the Loesung environment to be empty or not or to hide the content of the environment (see e.g. this post).
The only solution I can think of are
- use the
commentpackage - redefine the column separation macro as depicted in the MWE
To be honest, I have not tried the comment approach extensively but I'd rather avoid the issue with redefining the macros as one can quickly be caught by surprise.
So can anyone explain to me why the \matrix and \NewEnviron are incompatible or if I need to tweak some more (like make it fragile or so, do not know the technical details down to TeX)?
Edit 1:
As the answers seemed to be a bit off regarding my main question, I changed the MWE a bit and will give some more information here:
The intention is to have in fact two documents. One document is an exam or sheet for the students to work on. Of course, the solution should not be printed on this version. The other version is the solution for me and to give to the students for corrections This should contain both tasks as well as solutions.
Sorry, the word Lösung is German and means solution. I made Loesung out of it.
To realize the two similar documents, I have 3 ingredients: One file as the MWE but without the first line (\documentclass). I have two files that are just three or four lines (\documentclass, create and set a boolean and \input the central file). That way, the two versions are always in sync when compiled at the same time.
To avoid cluttering my complete central files with all sorts of control macros, I wanted to centralize the effort for hiding (and some other stuff) into an environment. In order to be able to switch on/off the contents, I need to use the \NewEnviron or I will get compile issues.
I found that the whole control logic is not the problem but the combination of \NewEnviron and TikZ (which alters the catcode of & in \matrix as I have learned) fails.
So: What can I do to make the \matrix work without remapping the ampersand?



multiaudiencepackage which allows to declare several environments. They will be printed or not according a general option. Something similar can be done withtcolorboxwhere yourtcblowerpart can be ignored or just not filled with some options. A possible example is https://tex.stackexchange.com/a/434156/1952 – Ignasi Mar 25 '21 at 15:15TikZinternally uses\pgfmatrixnextcell(see section 20.6 in pgfmanual) to separate cells.&or the definedampersand replacementare converted to\pgfmatrixnextcell. Did you try to use it with the\NewEnviron? By the way I don't understand the relation between your both questions. – Ignasi Mar 25 '21 at 15:20