1

I am writing a latex setup for exercise sheets.

I have defined a "solution" environment, that is only rendered if a boolean is set to true.

Sometimes, but not always, I will use a customised enumerate environment for different parts of an exercise. When i put the solution inside the enumerate, it will have a smaller linewidth.

How can i make it have the same linewidth as it would have outside of the enumerate?

MWE:

\documentclass[a4paper, 11pt]{article}
\usepackage{fullpage}
\usepackage{environ}
\usepackage{enumitem}
\usepackage{xcolor}

\setlength{\parindent}{0em}

\newcommand{\problem}[1]{\stepcounter{section}\section{Problem \sheetNr.\thesection: {#1}}} \newenvironment{subproblems}{\enumerate[label=\textbf{\alph}.]}{\endenumerate}

\newif\ifanswers \NewEnviron{solution}{ \ifanswers \color{purple} \par\mbox{}\hrulefill\newline \textbf{Solution}:\ % \expandafter\BODY % \fi }

\newcommand{\sheetNr}{1} \answerstrue

\begin{document} \problem{Some Title} A problem without subproblems \begin{solution} The solution has ``full" width. \end{solution}

\problem{Another Title}
\begin{subproblems}
    \item First part

    \begin{solution}
        This solution has reduced width
    \end{solution}

    \item Second Part
\end{subproblems}

\end{document}

1 Answers1

0

If you're willing to adjust the input slightly - using \problem and \subproblem as commands rather than a command/environment approach, you can terminate the list after every \subproblem and use the enumitem-provided resume key to resume your sub-problem lists.

enter image description here

\documentclass{article}

\usepackage{environ} \usepackage{enumitem} \usepackage{xcolor}

\setlength{\parindent}{0pt}

\makeatletter \def\restartlist{\relax}% \setlist[enumerate,1]{before=\restartlist} \newcommand{\problem}[1]{% \stepcounter{section}% \section{Problem \sheetNr.\thesection: #1}% \renewcommand{\restartlist}{\expandafter\setcounter\expandafter{@enumctr}{0}}% Restart numbering } \newcommand{\subproblem}[1]{% \begin{enumerate}[label=\textbf{\alph}.,resume] \item #1 \end{enumerate} \renewcommand{\restartlist}{\relax}% Don't restart numbering }

\newif\ifanswers \NewEnviron{solution}{% \ifanswers \par \setlength{\parskip}{0pt}% \color{purple}% \leavevmode\hrulefill\par\nobreak \textbf{Solution}: \par\nobreak \BODY \fi }

\newcommand{\sheetNr}{1} \answerstrue

\begin{document}

\problem{First problem} A problem without subproblems. \begin{solution} The solution has ``full'' width. \end{solution}

\problem{Second problem} \subproblem{First part}

\begin{solution} This solution has ``full'' width. \end{solution}

\subproblem{Second part}

\end{document}


If you wish to maintain the current input format (command \problem and environment subproblems), then you can capture the x-coordinate location and adjust the margins (using zref's savepos module and adjustwidth from changepage):

enter image description here

\documentclass{article}

\usepackage{environ} \usepackage{enumitem} \usepackage{xcolor} \usepackage{changepage,zref-savepos}

\setlength{\parindent}{0pt}

\newcommand{\problem}[1]{\stepcounter{section}\section{Problem \sheetNr.\thesection: #1}} \newenvironment{subproblems}{\enumerate[label=\textbf{\alph}.]}{\endenumerate}

\newif\ifanswers \newcounter{curanswer} \NewEnviron{solution}{% \ifanswers \par \stepcounter{curanswer}% \mbox{}\zsaveposx{leftmargin-\thecuranswer}% \par\vspace{\dimexpr-\parskip-\baselineskip}% \setlength{\parskip}{0pt}% \begin{adjustwidth}{\dimexpr\zposx{leftmargin}sp-\zposx{leftmargin-\thecuranswer}sp}{0pt} \color{purple}% \leavevmode\hrulefill\par\nobreak \textbf{Solution}: \par\nobreak \BODY \end{adjustwidth} \fi }

\newcommand{\sheetNr}{1} \answerstrue

\AtBeginDocument{% \zsaveposx{leftmargin}% }

\begin{document}

\problem{First problem} A problem without subproblems. \begin{solution} The solution has ``full'' width. \end{solution}

\problem{Second problem} \begin{subproblems} \item First part

\begin{solution} This solution has ``full'' width. \end{solution}

\item Second part \end{subproblems}

\end{document}

Werner
  • 603,163