2

I have used a variety of commands to hide answers to questions in documents. I use a boolean to to determine whether the material should be hidden, i.e. hideans True then hide, hideans False then don't.

\newbool{hideans} % True: Hide    False: Show

Usually, the hiding commands themselves work by something along the lines of turning the text white so it is invisible when printed:

\newcommand{\ans}[1]{%
    \ifbool{hideans}{\textcolor{white}{#1}}{#1}
}

Other versions use \phantom{} or similar approaches. The advantage is that the commands above keep the exact amount of whitespace that I would like to leave for people to put their answer.

The downside of these commands is that they not work well with line breaks, they do not work with figures and tables and can be fickle with equations. To get around this, I created a separate command for each of: figures, tables, inline equations, line breaks, etc.. Even then from time to time, I have to manually code in the whitespace. So my questions is...

Question. Is there a way of having LaTeX measure the size of the text, figures, equations, etc. enclosed by braces so that, as the command above, if a given boolean is true will leave that amount of whitespace between it and the next text and if the boolean is false print the material enclosed in the braces? Essentially, a 'command' like the one below

\ans{\ifbool{Hide?}{
     % Yes
     Measure size of figures, eqs, tables here and leave that amount of whitespace
  }{
     % No
     Then print the stuff in the braces before
   }

EDIT. As an example was requested, here is what I mean.

Question 1: What is 2 x 2?

[[[[Solution: Well, recall the following figure from Quantum Mechanics:
    \begin{figure}[!ht]
    \centering
    \includegraphics[width = \textwidth}{theimage.png}
    \end{figure}
This figure has nothing to do with the problem. But it's cute! \par

Now construct a figure of 1s representing the multiplication.
    \begin{table}[!ht]
    \centering
    \begin{tabular}{cccc}
    1 & 1 \\
    1 & 1 
    \end{tabular}
    \end{table}
So that we quickly see that $2 \times 2$ is
   \[
   2 \times 2= 4.
   \]

  ]]]]

What I am hoping for is something in LaTeX that will measure the amount of space that the material in the [[[[....]]]] consumes and if the boolean is set to True, hides it but leaves whitespace the size of what the text would have taken up, and if the boolean is False, just prints what I had typed for the solution.

  • If you are saying figures, do you mean floats? If so, how would that work? –  May 29 '19 at 23:36
  • @marmot Yes. The objects would not be a fixed size. I'm hoping there's a way for LaTeX to determine the size of the figures and include this in the total whitespace. – mathematics2x2life May 30 '19 at 01:05
  • @KJO Do you mean manually draft a whitebox size and then be sure everything fits inside it and use my appear/disappear function or a box where LaTeX measures the space and puts the box around it? It's really the latter I'm looking for. This perhaps is a simple solution? Boxes of any sort is not something I often use in TeX. – mathematics2x2life May 30 '19 at 01:12
  • @KJO I added an edit more showing what I am hoping to hide/unhide. Thanks for the time spent trying to sort out what I am looking for. – mathematics2x2life May 30 '19 at 02:16
  • But floats float, so how would that work? –  May 30 '19 at 02:32
  • @marmot Well so long as there room for them, then LaTeX typically places them right where they are. I suppose I am fine with functions which can't handle floats, because figures and tables can be done without their environments. But couldn't you handle the floats by forcing their placement with the float package? – mathematics2x2life May 30 '19 at 02:42
  • If it was not for the floats, a rather simple possibility would be to cover the unwanted stuff by some white foreground. The could work over page breaks etc. However, dealing with floats will be much more difficult and may only work by locally redefining them. –  May 30 '19 at 02:50
  • @KJO I don't know if the OP wants this because that may move the floats to other places as in the document without all this magic. –  May 30 '19 at 02:52
  • @marmot I'm really fine with most any solution that works with all the types (or can be made to work). It will probably be close to things i already do, sadly. I used to use `center' for centering for tables and pictures (avoiding using table or figure environ.) and then only had issues with pictures. But then one has to work around captions themselves. I just thought there should be easier ways that I was not aware of or wasn't thinking of. The suggestion of using Tikz or overlay to lay the tables, graphics, etc then overlaying a white square, I think, is not a bad idea. – mathematics2x2life May 30 '19 at 02:56
  • What is certainly possible is to modify e.g. this answer in such a way that everything between, say, \begin{hidden} and \end{hidden} is covered by a white foreground provided a, say, boolean hide is set true. The question is whether you want something of that sort. (Since these are overlay pictures, switching the foreground on and off won't change the document layout.) –  May 30 '19 at 03:01
  • @marmot I am not sure that I had something specific in mind. But certainly, using a tikz overlay would be a good solution because as you mentioned, it would not change the layout when the boolean is changed. It's also wonderful that it works over pagebreaks as well. Thanks for pointing out that altering that answer is a way of achieving a way of accomplishing what I was hoping for! – mathematics2x2life May 30 '19 at 03:07
  • @KJO Indeed. I have always avoided this by just posting PDFs post due date so as to avoid the issue. But I suppose if I am switching my commands, should I need in the future to do this, it would be better to make the change now. Is there a way to 'marry' both results? I am aware of \include and have used it in the past but don't quite see how I could have a solution elsewhere and have LaTeX add the space it takes up in the document anyhow? Or is this not what you meant? – mathematics2x2life May 30 '19 at 03:24

1 Answers1

1

You can decide whether or not this is helpful. All real problems are solved in this great answer by Loop Space, and the code requires a rather recent version of tikzmark, which comes with recent TeX installations or can be obtained from here. There is considerable room for improvement and tuning, but as a proof of principle the following may serve. Bon appetite!

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{etoolbox}
\newbool{hide}
%\url{https://tex.stackexchange.com/q/483547/86}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark}
\newcounter{tikzmarklines}\setcounter{tikzmarklines}{0} % 

\tikzset{
  tikzmark prefix=prefix-,
  tikzmark suffix=-suffix
}

\newcommand{\hidestart}{%
  \stepcounter{tikzmarklines}%
  \tikzmark{a\thetikzmarklines}%
  \iftikzmarkoncurrentpage{b\thetikzmarklines}%
  \else
  \begin{tikzpicture}[remember picture,overlay,next page=below]%
  \path (pic cs:a\thetikzmarklines) coordinate (start-\thetikzmarklines)
  ([yshift=-0.2ex]pic cs:b\thetikzmarklines) coordinate (end-\thetikzmarklines);
  \ifbool{hide}{\fill[white] (current page.west|-start-\thetikzmarklines)-- ++(0,1.2em) -|
  (current page.east|-end-\thetikzmarklines) -| cycle;}{}
  \end{tikzpicture}%
  \fi
}

\newcommand{\hidefinish}{%
  \tikzmark{b\thetikzmarklines}%
  \begin{tikzpicture}[remember picture,overlay,next page=below]%
  \path (pic cs:a\thetikzmarklines) coordinate (start-\thetikzmarklines)
  ([yshift=-0.2ex]pic cs:b\thetikzmarklines) coordinate (end-\thetikzmarklines);
  \ifbool{hide}{\fill[white] (current page.west|-start-\thetikzmarklines)-- ++(0,1.2em) -|
  (current page.east|-end-\thetikzmarklines) -| cycle;}{}
  \end{tikzpicture}%
  }

\begin{document} 
Text \hidestart text text 

Text text text 
\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{A duck.}
\end{figure}


Text text \hidefinish text

\newpage

Text text text 

Text \hidestart text text 

Text text text 
\clearpage
\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{A duck.}
\end{figure}


Text text text

Text text \hidefinish text
\clearpage\booltrue{hide}
Text \hidestart text text 

Text text text 
\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{A duck.}
\end{figure}

Text text \hidefinish text

\newpage

Text text text 

Text \hidestart text text 

Text text text 
\clearpage
\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{A duck.}
\end{figure}

Text text text

Text text \hidefinish text
\end{document}

enter image description here