I am relatively new to writing LaTeX packages. I would like to create an environment so that anything contained in that environment can be ignored when producing output. The desired behavior would be the following:
\documentclass{article}
\usepackage[hide]{MyPackage}
\begin{document}
This should always show up.
\begin{Hidden}
This should show up unless the [hide] option is used.
\end{Hidden}
\end{document}
One possible use case: if I write a problem set for a class, I can write the solutions into the same document wrapped in the Hidden environment. That way, I can compile with the [hide] option to produce the problem set for my students, and I can recompile without the [hide] option to produce the solution set.
Here's the code I currently use to accomplish this. It was at some point borrowed from a package offering similar functionality, but unfortunately I can't remember the original package.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{MyPackage}
\RequirePackage{ifthen}
\newboolean{MyPackage@hidden}
\setboolean{MyPackage@hidden}{false}
\DeclareOption{hide}{\setboolean{MyPackage@hidden}{true}}
\ProcessOptions\relax
\newenvironment{Hidden}
{\ifthenelse{\boolean{MyPackage@hidden}}
{\setbox\z@\vbox\bgroup}{}}
{\ifthenelse{\boolean{MyPackage@hidden}}{\egroup}{}}
This code has been working somewhat, but there are three main issues. The first is that there are errors when I put figures inside the Hidden environment. (This is a pretty common need when writing solutions to homework problems.) The following code does not compile:
\documentclass{amsart}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage[hidden]{hideexample}
\begin{document}
This always shows up.
\begin{Hidden}
\begin{figure}[h]
\begin{tikzpicture}
\draw (0,0) circle (2cm);
\end{tikzpicture}
\caption{This shows up when not hidden, but causes an error when the [hide] option is used.}
\end{figure}
\end{Hidden}
\end{document}
The second issue is that I would like to implement multiple hide-able environments. (Expanding on the homework use case, I might also want to create notes to the grader about how to assign partial credit. So I would want to be able to independently hide/show the solutions and the grader comments.) I have no idea how to adapt the code above to do this. I never really understood what the code does, just that it (partially) worked.
The third "issue" isn't really a problem with the functionality, but another feature. It would be really convenient if, instead of [hide]/[show] options, there was a [solutions] option (or something similar) so that compiling the document would produce both the homework set and the solution set at the same time. (So, I would be working on HW.tex, and compiling would produce HW.pdf and HW_solutions.pdf at the same time.) Is something like this possible?
Thanks in advance.
Follow-up: After modifying Werner's answer below, I'm using the code
\if@hidden
\long\gdef\hide[#1]#2\ehide{\vspace{#1}}%
\else
\long\gdef\hide[#1]#2\ehide{#2}%
\fi
This allows the following use:
\hide[4in]
%stuff
\ehide
This shows %stuff when I'm not using the global [hide] option. When I am using the [hide] option, %stuff is hidden, and 4 inches of blank vertical space are shown instead (as a placeholder for %stuff). The only issue is that this usage makes it look like [4in] is an optional argument. Since \def requires an exact calling match, the argument is actually required. How can I make this argument optional (i.e., with 0in as the default value)?
\hide[#1]#2\ehide
in the definition, you can use\hide#1#2\ehide
and use\hide{4cm} .. \ehide
unless you really wanted that[..]
in which case it's okey :)multiaudience
package allows to define environments that will be printed or not according a parameter. An example of use can be seen in 1 compilation, several outputs