# Option to hide text while maintaining synctex.gz functionality

My goal is the following: I would like to write exercises and their solutions in a single tex file with the option of hiding the solution, so that I do not need two separate tex files for the question file and the sample solution. In the tex file, I want to have something like

\documentclass{article}

\def\generateSolution{1} % 0 or 1

\usepackage{my_stylefile}

\begin{document}

... question ...

\solution{
...
}

\end{document}


Here, \solution is defined in my_stylefile and the solution is either shown or hidden depending on the value in \generateSolution (which gets accessed by the stylefile).

What is important to me is the functionality of the synctex.gz file, which enables some LaTeX editors to jump to the correct line in the pdf by clicking on the line in the tex file and vice versa. I have found ways to hide text, but none that preserve this functionality and can be moved to the stylefile.

The problem is that the synctex.gz file only recognizes the beginning and end of the block \solution{...}. I do not know a way around this.

Thus, it seems like the better approach to have something of the form

\begin{solution}

...

\end{solution}


Here, the comment package would come in handy. However, when I try somehting like the following in the stylefile

\usepackage{comment}

\if\generateSolution0

\newenvironment{solution}{\begin{comment}}{\end{comment}}

\else

\newenvironment{solution}{}{}

\fi


then I run into this error.

The best solution I have thus far is the following: stylefile:

\newcommand{\BeginSol}{\if\generateSolution1
{\large\textbf{\Solution:}}}


and tex file:

\BeginSol

solution text

\fi


However, this is not quite satisfactory as I cannot move the "\fi" in the stylefile as it has to appear explicitly in the tex file. Moreover, if I want to add cosmetical features, e.g. put the solution in an mdframed environment, then the "\end{mdframed}" will also appear in the tex file:

\newcommand{\BeginSol}{\if\generateSolution1
{\large\textbf{\Solution:}}\begin{mdframed}}

\BeginSol

solution text

\end{mdframed}\fi


Any suggestion is welcome! Cheers.

PS: As this is my first post on tex.stackexchange, I am not familiar with the appropriate tags.

New contributor
Florian R is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Welcome to TeX.SE! Sep 23 at 12:11
• Maybe it would be a good idea to use one of the packages for exercises and exams? They have built-in functionality for sowing and hiding solutions, which are easy to use - I'm not sure they work well with synctex but you could give it a try. See ctan.org/topic/exam for an overview, known packages are xsim, mcexam, hideanswer. Sep 23 at 12:15
• @Marijn Thanks for the suggestion, I will check them out! Sep 23 at 12:17
• In the versions with the hidden solutions, is it important that the text not be there at all, or just that it not be visible when printed? If, e.g., students only get it on paper, you might make it with white text on a white background so it's functionally invisible, but synctex would still work. But that's not a good solution if the students get the actual file, not just a printout, since they could select the text to see it or copy it. Sep 23 at 15:54

\excludecomment{solution} from the comment package does the trick:

\documentclass[]{article}
\def\generateSolution{1}

\usepackage{comment}
\if\generateSolution1
\newenvironment{solution}{\textbf{Solution:}}{}
\else
\excludecomment{solution}
\fi

\begin{document}
Always show this

\begin{solution}
I only want these paragraphs in the case showSolution is set to 1

BB

CC

DD

EE

FF

GG

HH

II

JJ

KK

LL

MM

NN
\end{solution}
\end{document}


The above solution works well for me in Overleaf. Double clicking a paragraph in the pdf preview of Overleaf makes the cursor jump to the correct paragraph in the TeX source code.

As far as I understood the documentation of comment the following alternative version should work as well, but in practice synctex.gz does not work for me in Overleaf for the following version:

\documentclass[]{article}
\def\generateSolution{1}

\usepackage{comment}
\if\generateSolution1
\includecomment{solution}
\else
\excludecomment{solution}
\fi

\begin{document}
Always show this

\begin{solution}
I only want these paragraphs in the case showSolution ist set to 1

BB

CC

DD

...
\end{solution}
\end{document}


I don't really understand why the second version does not work, but luckily the first version works well, so there is no need to use the second version.

These solutions are inspired from the answers https://tex.stackexchange.com/a/3025/128042, https://tex.stackexchange.com/a/3026/128042 and https://tex.stackexchange.com/a/3073/128042 to the question Latex tag for making a comment appear or disappear in pdf?.