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.