5

How do I insert a custom (constant) text before all item descriptions in a description list?

For instance, I want to create something like this:

 Question 1: My question
 Answer: My answer

Generated from a code like:

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

I'm using enumitem to edit some parts of the description list. My code until now follows:

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

With such a code a get something like:

Question 1: My question
My answer

But I can't find how to put the "Answer" word before the actual answer. Also, follows a minimal working example:

\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}
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Feb 25 '15 at 20:54
  • I would use an interface that resembles \begin{questionsandanswers} \newquestion This is the question? \newanswer This is the answer. \newquestion This is another question? \newanswer This is another answer. \end{questionsandanswers}, or even simplify it to \question and \answer. Are you open to such a suggestion? – Werner Feb 25 '15 at 21:36
  • @Werner I though of such solution, but it is more loose than my desired syntax. By replacing the \item by two separated commands I let go off the need for an actual answer. One can put several \question without appearing a single \answer. Other solution would be an \questionanswer{My Question}{My Answer} (two parameters), but I really want to know if it can be done using the \item ... – Isma Feb 25 '15 at 21:44
  • @Isma: You can force the user to require a \newanswer with every \newquestion, thereby not "let[ting] go of the need for an actual answer". – Werner Feb 25 '15 at 21:56
4

Here's a rather hacky way to achieve your desired output:

enter image description here

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

I'd much rather prefer the following setup though:

enter image description here

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

The above doesn't force the user to have a \newanswer with every \newquestion. However, you can force this with the following definitions \newquestion:

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

The parameter text of \newquestion now includes \newanswer. As such, without it in the input stream, TeX will complain, forcing the user to have a \newanswer with every \newquestion.

0

try this code

\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}
  • I tried your solution and it was almost effective. However, I couldn't put a line break before "Answer". I tried to put \normalfont\bfseries Question \thedescriptcount.~ #1 \\ Answer: }, also tried \normalfont\bfseries Question \thedescriptcount.~ #1 \newline Answer: } (both with and withoud \hfill). – Isma Feb 25 '15 at 21:52
  • Yes this is enside sbox so no line break here – touhami Feb 25 '15 at 22:44
  • @Isma I edit my answer. – touhami Feb 25 '15 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.