1

In the following MWE

\documentclass{article}
\usepackage{amsmath, environ}

\NewEnviron{hidden}{}
\def\hide#1{}

\begin{document}
\begin{align*}
  X &= Y \\
  % \begin{hidden}  % DOES NOT WORK
    &=Y_{1}%
  \\ &\leq Y_{2}\\%
  % \end{hidden}
  \hide{&=Y_{3}\\&=Y_{4}}
    &\le Z
\end{align*}
\begin{hidden}
  Some more tests
\end{hidden}
\end{document}

I define a command and an environment to hide a certain part of the document (this is a simplification, my use case is a bit more complicated). The command works in all situation including an align environment. However, my new environment does not work inside align or similar commands. This is probably because \begin{hidden} \end{hidden} opens and closes a group within aligned which creates problems. Is there a way to create a dummy environment that does not create groups, much like a command seemingly does not.

3
  • even if you stopped your environment forming a group it would fail because each alignment cell between &...& or &...\\ is a group so whatever you set in \begin{hidden} will be lost at the first & (you will see same in tabular or array and similar environments, not just align Nov 18, 2020 at 11:16
  • Thanks David. I am not sure I get your point 100%. Does this apply to commands as well (like my \hide)? If not, I don't understand why an environment would be different from a command, except for the introduction of the new group.
    – Tohiko
    Nov 18, 2020 at 11:22
  • the {} in the command syntax automatically "hide" the & so as you skip over hat section the alignment cell does not end. For nested environemnts you have to work a bit harder (using the same special groups that are used so you can nest array or aligned inside align without the & ending the outer alignment Nov 18, 2020 at 11:32

2 Answers 2

2

You actually want to add a group so that when you skip over & you do not end the alignment cell and so end the group where you started hiding.

enter image description here

This version does not cope with nested environments, but works here and shows teh basic scheme.

\documentclass{article}
\usepackage{amsmath, environ}

\def\hidden{{\ifnum0=`}\fi
\xhidden}
\def\xhidden#1\end#2{\ifnum0=`{\fi}\end{hidden}}% probably ought to check that #2 is "hidden"


\def\hide#1{}

\begin{document}
\begin{align*}
  X &= Y \\
   \begin{hidden}  % DOES NOT WORK
    &=Y_{1}%
  \\ &\leq Y_{2}\\%
   \end{hidden}
  \hide{&=Y_{3}\\&=Y_{4}}
    &\le Z
\end{align*}
\begin{hidden}
  Some more tests
\end{hidden}
\end{document}
2
  • This is great! I want to modify this to cope with nested environments. But I admit I don't fully understand the solution (my LaTeX-fu is limited). What does \ifnum0={\fi} do in both \hidden and \xhidden?
    – Tohiko
    Nov 18, 2020 at 11:38
  • Found this which shows that this syntax starts and ends a group that could contain & in the argument. Interesting.
    – Tohiko
    Nov 18, 2020 at 12:06
0

Based on David's answer. Here's my attempt to make things work for nested environments.

\documentclass{article}
\usepackage{amsmath, etoolbox}

\def\hide#1{} % or \def\hide#1{#1} to not hide it.

% Essentially flips "\begin{hidden}#1\end{hidden}" to "\begin{hidden}\end{hidden} \hide{#1}"
\def\hidden{{\ifnum0=`}\fi\xhidden\argend}%
\def\xhidden#1\argend#2\end#3{%
\expandafter\ifstrequal\expandafter{#3}{hidden}%
{\ifnum0=`{\fi}\end{hidden}\hide{#1#2}}{\xhidden#1#2\end{#3}\argend}}

\begin{document}
\begin{align*}
  X &= Y \\
  \begin{hidden}
    &=Y_{1}%
    \\ &\leq Y_{2}\\%
    \begin{matrix}
      A & BC
    \end{matrix}\\
  \end{hidden}
  \hide{&=Y_{3}\\&=Y_{4}}
    &\le Z
\end{align*}
\begin{hidden}
  \begin{center}
    Hello
  \end{center}
  Some more tests
\end{hidden}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .