TransWikia.com

Custom environment with full line width, even when put in e.g. enumerate

TeX - LaTeX Asked by Jakob Unfried on June 30, 2021

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}

newififanswers
NewEnviron{solution}{
    ifanswers
        color{purple}
        parmbox{}hrulefillnewline
        textbf{Solution}:
        %
        expandafterBODY
        %
    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}

One Answer

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
defrestartlist{relax}%
setlist[enumerate,1]{before=restartlist}
newcommand{problem}[1]{%
  stepcounter{section}%
  section*{Problem sheetNr.thesection: #1}%
  renewcommand{restartlist}{expandaftersetcounterexpandafter{@enumctr}{0}}% Restart numbering
}
newcommand{subproblem}[1]{%
  begin{enumerate}[label=textbf{alph*}.,resume]
    item #1
  end{enumerate}
  renewcommand{restartlist}{relax}% Don't restart numbering
}

newififanswers
NewEnviron{solution}{%
  ifanswers
    par
    setlength{parskip}{0pt}%
    color{purple}%
    leavevmodehrulefillparnobreak
    textbf{Solution}: parnobreak
    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}

newififanswers
newcounter{curanswer}
NewEnviron{solution}{%
  ifanswers
    par
    stepcounter{curanswer}%
    mbox{}zsaveposx{leftmargin-thecuranswer}%
    parvspace{dimexpr-parskip-baselineskip}%
    setlength{parskip}{0pt}%
    begin{adjustwidth}{dimexprzposx{leftmargin}sp-zposx{leftmargin-thecuranswer}sp}{0pt}
      color{purple}%
      leavevmodehrulefillparnobreak
      textbf{Solution}: parnobreak
      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}

Correct answer by Werner on June 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP