描述列表中实际描述之前的文本

描述列表中实际描述之前的文本

如何在描述列表中的所有项目描述前插入自定义(常量)文本?

例如,我想创建如下内容:

 Question 1: My question
 Answer: My answer

从如下代码生成:

\begin{questionsandanswers}
    \item[My question] My Answer
\end{questionsandanswers}

我正在使用 enumitem 编辑描述列表的某些部分。到目前为止,我的代码如下:

    \usepackage{enumitem}

    \newcounter{descriptcount}
    \newlist{enumdescript}{description}{2}
    \setlist[enumdescript,1]{%
      before={\setcounter{descriptcount}{0}%
              \renewcommand*\thedescriptcount{\arabic{descriptcount}}}
      ,font=\textbf{\stepcounter{descriptcount}Question \thedescriptcount:~} \normalfont
    }
    \setlist[enumdescript,2]{%
      before={\setcounter{descriptcount}{0}%
              \renewcommand*\thedescriptcount{\alph{descriptcount}}}
      ,font=\textbf{\stepcounter{descriptcount}Question \thedescriptcount:~} \normalfont
    }

    \newenvironment{questionsandanswers}{\begin{enumdescript}[style=nextline,labelindent=\parindent,leftmargin=\parindent]}{\end{enumdescript}}

使用这样的代码可以得到如下结果:

Question 1: My question
My answer

但我不知道如何在实际答案之前添加“答案”一词。此外,下面是一个最小的工作示例:

\documentclass{article}[12pt,a4paper]

\usepackage[T1]{fontenc} 
\usepackage[utf8]{inputenc}
\usepackage{enumitem}


\newcounter{descriptcount}
\newlist{enumdescript}{description}{2}
\setlist[enumdescript,1]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\arabic{descriptcount}}}
  ,font= \textbf{\stepcounter{descriptcount}Question \thedescriptcount.~} \normalfont
}
\setlist[enumdescript,2]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\alph{descriptcount}}}
  ,font= \textbf{\stepcounter{descriptcount}Question \thedescriptcount.~} \normalfont
}


\newenvironment{questionsandanswers}{\begin{enumdescript}[style=nextline,labelindent=\parindent,leftmargin=\parindent]}{\end{enumdescript}}

\begin{document}

Desired syntax:

\begin{questionsandanswers}

    \item[My question] My answer

\end{questionsandanswers}

Desired result:

\begin{questionsandanswers}

    \item[My question] \textbf{Answer:} My answer

\end{questionsandanswers}


\end{document}

答案1

这是一个实现所需输出的相当棘手的方法:

在此处输入图片描述

\documentclass{article}

\usepackage{enumitem}

\newcounter{descriptcount}
\newlist{enumdescript}{description}{2}
\setlist[enumdescript,1]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\arabic{descriptcount}}}
  ,font= \textbf{\stepcounter{descriptcount}Question \thedescriptcount.~} \normalfont
}
\setlist[enumdescript,2]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\alph{descriptcount}}}
  ,font= \textbf{\stepcounter{descriptcount}Question \thedescriptcount.~} \normalfont
}


\newenvironment{questionsandanswers}
  {% \begin{questionsandanswers}
    \renewcommand{\descriptionlabel}[1]{%
      \hspace\labelsep
      \normalfont\bfseries##1}%
      \let\oldnewline\newline
      \renewcommand{\newline}{\oldnewline\textbf{Answer:}\hspace\labelsep}
    \begin{enumdescript}[style=nextline,labelindent=\parindent,leftmargin=\parindent]
  }{% \end{questionsandanswers}
    \end{enumdescript}
  }

\begin{document}

Desired syntax:

\begin{questionsandanswers}
  \item[My question] My answer
\end{questionsandanswers}

\end{document}

但我更喜欢以下设置:

在此处输入图片描述

\documentclass{article}

\usepackage{enumitem}

\newcounter{descriptcount}
\newlist{enumdescript}{description}{2}
\setlist[enumdescript,1]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\arabic{descriptcount}}}
  ,font= \bfseries
}
\setlist[enumdescript,2]{%
  before={\setcounter{descriptcount}{0}%
          \renewcommand*\thedescriptcount{\alph{descriptcount}}}
  ,font= \bfseries
}
\newcommand{\newquestion}{%
  \stepcounter{descriptcount}%
  \item[Question~\thedescriptcount.]}
\newcommand{\newanswer}{%
  \par\vspace{-.5\baselineskip}
  \item[Answer:]}

\newenvironment{questionsandanswers}
  {% \begin{questionsandanswers}
    \begin{enumdescript}[labelindent=\parindent,leftmargin=\parindent]
  }{% \end{questionsandanswers}
    \end{enumdescript}
  }

\begin{document}

Desired syntax:

\begin{questionsandanswers}
  \newquestion My question
  \newanswer My answer
  \newquestion Another question
  \newanswer Another answer
\end{questionsandanswers}

\end{document}

上面的代码并不强制用户必须使用\newanswerevery \newquestion。但是,你可以使用以下定义来强制执行此操作\newquestion

\long\def\newquestion#1\newanswer{%
  \stepcounter{descriptcount}%
  \item[Question~\thedescriptcount.] #1
  \newanswer}

的参数文本\newquestion现在包括\newanswer。因此,如果输入流中没有它,TeX 将会报错,迫使用户在\newanswer每个 中都使用\newquestion

答案2

尝试这个代码

\documentclass{article}[12pt,a4paper]

\usepackage[T1]{fontenc} 
\usepackage[utf8]{inputenc}
\makeatletter   
\newcounter{descriptcount}
\newenvironment{questionsandanswers}{\list{}{%
\labelwidth0pt\itemindent-\leftmargin
              \let\makelabel\qudescriptionlabel}
              \def\item[##1]{%
              \@inmatherr\item
              \@item[##1]\ \par\hspace{-\leftmargin}\textbf{Answer:}}}
               {\endlist}
\makeatother
\newcommand\qudescriptionlabel[1]{%
            \refstepcounter{descriptcount}\hspace\labelsep
            \normalfont\bfseries Question \thedescriptcount.~ #1}


\begin{document}

相关内容