3

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 comment package
  • 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?

  • Problem solved? If yes, please upvote/accept an answer. – Dr. Manuel Kuehner Mar 24 '21 at 19:20
  • Not really, I updated the question with more details as it seems to me that the answers were a bit off track. – Christian Wolf Mar 25 '21 at 12:15
  • Then you should leave a comment below each answer. Otherwise the authors of the answers don't get notified. – Dr. Manuel Kuehner Mar 25 '21 at 14:36
  • if I've well understood your Edit 1 part, you need to show|print or not solutions. Here you have two possible solutions: https://tex.stackexchange.com/a/273781/1952 this one uses multiaudience package which allows to declare several environments. They will be printed or not according a general option. Something similar can be done with tcolorbox where your tcblower part 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:15
  • TikZ internally uses \pgfmatrixnextcell (see section 20.6 in pgfmanual) to separate cells. & or the defined ampersand replacement are 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

2 Answers2

3

If you want to compose twice the body of an environment, you have to take that content as an argument as if it were the argument of a command (whereas a standard LaTeX environment only adds code before and after the body).

However, it's not possible to put a Tikz matrix inside the argument of a command because Tikz change the catcode of the ampersand &.

Nevertheless, there is a workaround: writing the body of the environment in an external file and read that file twice.

\documentclass{article}

\usepackage{verbatim} \usepackage{fancyvrb}

\ExplSyntaxOn \makeatletter \cs_generate_variant:Nn \iow_now:Nn { N V } \iow_new:N \l__pantigny_stream

\NewDocumentEnvironment{MyEnv}{} { \group_begin: \iow_open:Nn \l__pantigny_stream { correction.tex } \tl_map_function:nN { \ \ { } $ & # ^ _ % ~ } \char_set_catcode_other:N \char_set_catcode_active:N ^^M \cs_set:Npn \verbatim@processline { \iow_now:NV \l__pantigny_stream \verbatim@line } \verbatim@start } { \iow_close:N \l__pantigny_stream \group_end: \VerbatimInput { correction.tex } \file_input:n { correction.tex } } \makeatother \ExplSyntaxOff

\usepackage{tikz} \usetikzlibrary{circuits.logic.IEC} \tikzset{>=stealth,circuit logic IEC}

\begin{document}

\begin{MyEnv} \begin{tikzpicture} \matrix[column sep=7mm]% { \coordinate (A); \ & \node [nand gate] (g1) {} ; & \node [nand gate] (g2) {}; & \coordinate (out); \ \coordinate (B); \ }; \end{tikzpicture} \end{MyEnv}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
3

tcolorbox was initially developed to show latex code and its result so, why not to use it?

Following code is just an initial idea to show how can it work. Loesung is now a tcblisting environment where upper part will show the tikzpicture and lower part the corresponding latex code. But you can use lowerbox=ignored option to show or not to show the latex code. This option can be applied to all code fragments or be introduced as an option for every example.

Of course all box aspect and listing+results distribution can be changed to you taste.

This is the code:

\documentclass{scrartcl}
\usepackage[most]{tcolorbox}

\tcbset{lowerbox=ignored}

\newtcblisting{Loesung}{enhanced, text and listing}

\usepackage{tikz} \usetikzlibrary{circuits.logic.IEC} \tikzset{>=stealth,circuit logic IEC} \begin{document} \begin{Loesung} \begin{tikzpicture} \matrix[column sep=7mm]%,ampersand replacement=&]% { \coordinate (A); \ & \node [nand gate] (g1) {} ; & \node [nand gate] (g2) {}; & \coordinate (out); \ \coordinate (B); \ }; \end{tikzpicture} \end{Loesung} \end{document}

This is the result with \tcbset{lowerbox=ignored} active

enter image description here

and this with previous line commented

enter image description here

Ignasi
  • 136,588