3

Excuse me for my bad English. I want to make answer part of page unvisible but occupy space. I use xetex.

\documentclass{book}
\usepackage{lipsum}
\newcommand{\question}[1]{#1}
\newcommand{\answer}[1]{#1}
\begin{document}
    \question{What is your idea?}
    \answer{\lipsum[1]}
    \lipsum[2]
\end{document}

Output: enter image description here In above code I want to lipsum1 be unvisible. like this enter image description here Unfortunately transparent package not work in xetex and since answer part might be everything like tikz or colorbox or other things so change color of answer part to white not solve the problem in some situations. Thanks in advance

4
  • 3
    „Invisible” in the printout / PDF display only or should the content itself not be part of the PDF file? – Daniel Jun 23 '18 at 4:07
  • @cfr thanks but phantom dont solve problem – mojtaba baghban Jun 23 '18 at 6:10
  • @Daniel thanks. content of answer must be invisible – mojtaba baghban Jun 23 '18 at 6:13
  • 4
    There is a difference between invisible but present (transparent, white, copy & paste is possible) and invisible occupying space, but not being there (phantom, no copy & paste). – Heiko Oberdiek Jun 23 '18 at 10:31
2

Option 1: Nullify Everything and Measure Using Boxes

If you want to make anything (any box) invisible, you could

  • wrap things with an environment
  • capture the content in a box that you never typeset (just measure)
  • measure the box's height,
  • add a vertical skip of that amount

The output might not be exactly the same as the visible version due to line skipping issues, but it should be within a reasonable margin of error.

Code

\documentclass{book}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{environ,varwidth}
\newcommand{\question}[1]{#1}

\newcounter{showsolutions}
\setcounter{showsolutions}{0}% 0=False 1=True

\newsavebox{\hidebox}
\NewEnviron{answer}{\savebox{\hidebox}{\begin{varwidth}{\linewidth}\BODY\end{varwidth}}\ifnum\value{showsolutions}=0\relax\par\vspace{\the\dimexpr\ht\hidebox+\dp\hidebox}\else\BODY\fi}%

\begin{document}
  \question{What is your idea?}
  \begin{answer}
  \lipsum[1]
  \end{answer}
  \lipsum[2]
\end{document}

Option 2: Whitify Text

For a text-only solution

If

  • it is just text you‘re looking to hide,
  • you want the text to take up the same amount of space as it normally would
  • should work with xelatex

swapping out black for white should do what you want.

\documentclass{book}
\usepackage{lipsum}
\usepackage{xcolor}
\newcommand{\question}[1]{#1}
\newcommand{\answer}[1]{\begingroup\color{white}#1\endgroup}
\begin{document}
  \question{What is your idea?}
  \answer{\lipsum[1]}
  \lipsum[2]
\end{document}

enter image description here

Another way might be to have a look at the xetex documentation, particularly under the font options. You could define.

enter image description here

7
  • Thanks for your attention. but it is not just text I'm looking to hide. answer argument might be tikz or picture or math or text or everything else. so just change color to white not solve all of situation. make transparent to 0 solve all situation but transparent package is not compatible with xetex. – mojtaba baghban Jun 23 '18 at 11:02
  • @mojtababaghban Without example code, it is hard to help. Maybe you could, at least, list the conditions in the question – Jonathan Komar Jun 23 '18 at 12:41
  • @mojtababaghban See update. – Jonathan Komar Jun 23 '18 at 13:03
  • Thanks alot. I want when answer is invisibe page be like when it is visible. I want answer have an argument and depend on that argument answer be visible or invisible and occupid space when answer is invisible be exactly like when answer is visible. – mojtaba baghban Jun 23 '18 at 13:26
  • Consider a situation that answer finish at the same line question exist so in this situation vspace not need just not typeset answer solve problem. some situation answer start at middle of a line and other situations. – mojtaba baghban Jun 23 '18 at 13:33
2

A most straightforward solution would be to use the adjustbox package and define answer command as :

 \newcommand{\answer}[1]{\adjustbox{minipage=linewidth,phantom,frame}{#1}}

and removing the phantom option when the answer should be displayed. Of course, the frame is for demonstration purpose and must likely be removed.

Edit: I just read in the comments that the switch must be given as argument, so \answer can be defined as:

\newcommand\answer[2][phantom]{\adjustbox{minipage=\textwidth,frame,#1}{#2}}

with an optional argument, set by default to phantom to hide the content, and simply omitted to show it.

Nevertheless, as a teacher, I would find much more convenient to make a global change, for example with a simple \newif that could be used as follows.

\documentclass{book}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{adjustbox}
\newcommand{\question}[1]{#1\par}

\newif\ifhideanswer

\newcommand\answer[1]{%
\ifhideanswer
\adjustbox{minipage=\textwidth,phantom}{#1}
\else
\adjustbox{minipage=\linewidth}{#1}
\fi
}

\begin{document}
\hideanswerfalse
\question{What is your idea?}
\answer{%
\tikz{\draw[fill=red,line width=1pt]  circle(1ex);} 
\lipsum[1]
}
\lipsum[2]

\rule{\textwidth}{1pt}  \bigskip

\hideanswertrue
\question{What is your idea?}
\answer{
\tikz{\draw[fill=red,line width=1pt]  circle(1ex);} 
\lipsum[1]
}
\lipsum[2]

\end{document}

producing the attached image. enter image description here

6
  • consider situation that answer and question are in same line – mojtaba baghban Jun 23 '18 at 14:56
  • If you really need this, I will have a try to provide it as an additional argument to answer, containing the question... – Jhor Jun 23 '18 at 19:56
  • @Jhor With \ifvmode you can check wether you are on a new paragraph or still in the same paragraph. – Manuel Jun 23 '18 at 20:51
  • @Manuel, thanks for the hint, but I don't understand what would be the use here. My adjustbox solution likely wont use it as the box is not breakable and the 'phantom' option affect it as a whole. – Jhor Jun 24 '18 at 17:41
  • @Jhor In case you were going to implement the inline answer solution. – Manuel Jun 24 '18 at 21:13
0

You could use boxes to make sure the height is appropriate. That is, the actual height of the content. Almost.

By defining the \ProcessCutFile to be empty then the comment package won't do anything (it's all in the docs). Further more, we can use the \CommentCutFile to put the content of the environment into a \box, which we can get the height of. Then we just apply \vskip to push this height.

This does not make space across multiple pages (although, I made a similar answer here that does). Also it works with verbatim commands like listings

\documentclass{article}
\usepackage{comment}
\usepackage{lipsum}
\usepackage{tikz}
% Make ourselves a new conditional
% Use \HideSolutionstrue to "activate" it
\newif\ifHideSolutions
% Make a solution environment
\generalcomment{solution}{%
  \begingroup
  \ifHideSolutions%
  % if \HideSolutionstrue is not called, then we remove the contents
  \def\ProcessCutFile{}\fi%
}{%
  \ifHideSolutions%
    % aand,now (also when it's not called), we make a box
    % and then we \input the \CommentCutFile.
    \setbox1=\vbox{\input{\CommentCutFile}}%
    % Get the height from \ht1 and use \vskip to make appropriate space
    \vskip\ht1
  \fi
\endgroup%
}

%Uncomment below to show solutions
\HideSolutionstrue

\begin{document}
  Some problem text goes here

  \begin{solution}
    \textbf{SOLUTION 1}
    \[ \forall\varepsilon>0\exists\delta>0:\dots \]
    \tikz{\draw(0,0)--(3,2);}
\end{solution}
%^ \end{solution} has to be ALONE on line without any prior spaces

  BETWEEN

\begin{solution}
    \textbf{SOLUTION 2}
    \begin{verbatim}
        This
        is
        also
        hidden
    \end{verbatim}
    \[ \forall\varepsilon>0\exists\delta>0:\dots \]
\end{solution}
%^ has to be ALONE on line without any prior spaces
  Some text after solution.

\end{document}

Not the answer you're looking for? Browse other questions tagged or ask your own question.