标记问题环境

标记问题环境

我的最低代码

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
    frametitlebackgroundcolor=green!40,
    frametitlerule=true
}
\newenvironment{question}[1]{%
    \begin{mdframed}[style=ans,frametitle={Question: #1}]
}{%
    \end{mdframed}%
}%

\begin{document}
\begin{question}
\label{masi} % PROBLEM here, since label not working!
{Question body here? What is your question?}
Answer is this and this. Lorem yea.   
\end{question}
\end{document}

它的发展源于讨论但标签不起作用。

在这样的环境中如何使用标签?

答案1

命令必须有一个计数器\label,我引入了一个名为 的新计数器 question。理想情况下,计数器也应该打印在问题上,我现在已经添加了它,参考也有效。

\label使用最后一个计数器的值,该值已增加\refstepcounter。如果代码中有许多部分,则使用可能很方便,\newcounter{question}[section]以便重置它并重新定义\thequestion命令以

\renewcommand{\thequestion}{\arabic{section}.\arabic{question}

由于没有部分,我省略了重新定义。

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
    frametitlebackgroundcolor=green!40,
    frametitlerule=true
}

\newcounter{question}[section]%
\setcounter{question}{0}

\newenvironment{question}[1]{%
\refstepcounter{question}%
    \begin{mdframed}[style=ans,frametitle={Question \thequestion: #1}]
}{%
    \end{mdframed}%
}%

\begin{document}
\begin{question}
 % PROBLEM here, since label not working!
{Question body here? What is your question?}
\label{masi}
Answer is this and this. Lorem yea.   
\end{question}

\begin{question}
 % PROBLEM here, since label not working!
{Question body here? What is your question?}
\label{masi2}
Answer is this also. Lorem yea.   
\end{question}


The Question \ref{masi} was really tricky, but Question \ref{masi2} was really tricky ;-)




\end{document}

在此处输入图片描述

答案2

也许你可以考虑tcolorbox。它提供了一个theorems简化枚举彩色框的定义和使用的库。

\newtcbtheorem[<init options>]{<name>}{<display name>}{<options>}{<prefix>}

定义一个新的name枚举环境(不用担心计数器,包会处理它们)。display name将出现在数字之前,options定义环境样式,并将prefix在内部添加为所有使用的标签的前缀。定义后,您可以在文本中使用

\begin{name}{Some description text}{label}
....
\end{name}

Some description text这将创建一个带有标题框、...下部框和标签的盒装环境prefix:label,以供进一步参考。这里有一个小例子

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{
    questionstyle/.style={enhanced, colback=white, colframe=blue!20, arc=0pt, 
                                 fonttitle=\bfseries, colbacktitle=green!40,coltitle=black, 
                                 boxed title style={arc=0pt}}
    }

\newtcbtheorem[]{myquestion}{Question}{questionstyle}{quest}

\begin{document}

\begin{myquestion}{Question Body goes here. As you can see this is a very long question. At least its title is. What's your question?}{masi}
This is my answer
\end{myquestion}

\begin{myquestion}{New question related to question \ref{quest:masi}}{masi2}
This is the answer to question \ref{quest:masi2} which was related with question \ref{quest:masi}
\end{myquestion}
\end{document}

在此处输入图片描述

相关内容