带有 tcolorboxes 和条件排版的 NewDocumentEnvironment

带有 tcolorboxes 和条件排版的 NewDocumentEnvironment

对于我的课程作业,我想创建一个Exercise问题陈述的环境(继续提出的想法这里) 和另一个环境,只有当为Answer时才会排版。下面是一个(不那么简单的) MWE:AnsReq \newiftrue

\documentclass[12pt]{article}
\setlength{\parindent}{0bp} %No indent wanted

\usepackage{xparse}
\usepackage{tcolorbox}
\tcbset{boxsep=0pt,tcbox raise base}

\newtcolorbox[auto counter] {Exercise}[1][]{%
  before upper={
    \renewcommand\thempfootnote{\fnsymbol{mpfootnote}}
    {\makebox[2em][l]{\large\bf\thetcbcounter.~}}
    },#1
  }

\newif\ifAnsReq                     %If it's true, then typeset answers 
\NewDocumentEnvironment { Answer }
  { +b }
  { \ifAnsReq
    \tcbox[colupper=blue!50!black]{\sc Answer:}\par
    \begin{tcolorbox}[colback=white,colframe=white]
      {#1}
    \end{tcolorbox}
    \fi
  }
  {}

\usepackage{tikz}
\usetikzlibrary{babel,quotes,arrows.meta,positioning}
\tikzset{baseline=(current bounding box.center),>=Latex,
  ampersand replacement=\&,                                %Must use this!
  }

\begin{document}
\AnsReqtrue %comment if don't want to typeset the answer

\begin{Exercise}[label={ej:1}]
  An exercise\footnote{This is the footnote} with a footnote.
\end{Exercise}

\begin{Answer}
  The answer:
  \[
    \begin{tikzpicture}
      \matrix[
        every node/.style={
        circle,draw,fill,inner sep=1.5pt},
        row sep=2.5em,
        column sep=2.5em
      ] {
        \node (one)  {}; \& \node (two) {}; \\
        };
        \path (one) edge (two);
        \node[left=0pt of one] {$1$};
        \node[right=0pt of two] {$2$};
    \end{tikzpicture}
  \]
\end{Answer} 

\begin{Exercise}
    Another exercise (see \textbf{exercise \ref{ej:1}}).
\end{Exercise}

\begin{Answer} I don't know anything about \TeX! \end{Answer}
\end{document}

没有的排版Answer没有答案排版

设置AnsReq打印true答案: 答案排版 虽然所示的代码执行了所建议的任务,但我认为它有一些缺点,例如,矩阵ampersand replacement中的选项tikz是必要的(例如,参见:)我想知道是否有可能以更优雅的方式开发这些想法,并且不会出现上述不便。

相关内容