具有全线宽度的自定义环境,即使放入枚举中

具有全线宽度的自定义环境,即使放入枚举中

我正在为练习表编写乳胶设置。

我已经定义了一个“解决方案”环境,只有当布尔值设置为 true 时才会呈现。

有时,但不总是,我将使用自定义枚举环境来完成练习的不同部分。当我将解决方案放入枚举中时,它的线宽会更小。

我怎样才能使其具有与枚举外部相同的线宽?

梅威瑟:

\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

如果你愿意稍微调整输入 - 使用\problem\subproblem作为命令而不是命令/环境方法,你可以在每个之后终止列表\subproblem并使用enumitem- 提供resume密钥来恢复您的子问题列表。

在此处输入图片描述

\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}

如果您希望保留当前输入格式(命令\problem和环境subproblems),那么您可以捕获 x 坐标位置并调整边距(使用zrefsavepos模块和adjustwidth来自changepage):

在此处输入图片描述

\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}

相关内容