2

I'm trying to design a basic "interview format" consisting on a series of questions and answers, which I want to typeset in different styles like, for instance, boldface for questions with a leading "Q:" and normal text for answers with a leading "A:"

so far I've the following code that produces what I want, but is a bit cumbersome:

\begin{list}{}{}
   \item[\textbf{Q:}] \textbf{This is the first question}
   \item[A:] And this is the answer
   \item[\textbf{Q:}] \textbf{This is the second question}
   \item[A:] Another answer
\end{list}

The result is:

enter image description here

I even managed to define a new environment and a couple of commands to save typing:

\newenvironment{interview}
  {\begin{list}{}{}}
  {\end{list}}

\newcommand{\question}[1]{\item[\textbf{Q:}] \textbf{#1}}
\newcommand{\answer}[1]{\item[A:] #1}

\begin{interview}
   \question {This is the first question}
   \answer {And this is the answer}
   \question {This is the second question}
   \answer {Another answer}
\end{interview}

This satisfies me enough, but I wonder if that could have been defined in any other way to avoid enclosing the argument of each "item" between curly braces, like we do in normal "itemize" lists. Put otherwise: Is it possible to define things in a way that one can write something like this??:

\begin{interview}
   \question This is the first question
   \answer And this is the answer
   \question This is the second question
   \answer Another answer
\end{interview}

2 Answers 2

3

Start \bfseries for questions and restore \normalfont for answers.

\documentclass{article}

\newenvironment{interview}
  {\begin{list}{}{}}
  {\end{list}}

\newcommand{\question}{\bfseries\item[Q:]}
\newcommand{\answer}{\normalfont\item[A:]}

\begin{document}

\begin{interview}
\question This is the first question

\answer And this is the answer

\question This is the second question

\answer Another answer
\end{interview}

\end{document}

The blank lines between the items are a personal preference; I believe they're better than indenting the code.

enter image description here

2
  • Ah! simple but elegant solution. Does what I wanted. I should have realized myself... Commented Oct 7, 2018 at 21:59
  • @OrestesMas After all, each question is followed by an answer… ;-)
    – egreg
    Commented Oct 7, 2018 at 22:00
2

I would use the enumitem package to define a new list style, so that you could type

  \begin{interview}
    \item This is the first question
    \item And this is the answer
    \item This is the second question
    \item Another answer
  \end{interview}

to produce

enter image description here

The main thing you need to do is to change the default behaviour of the \item command so that it puts in your Q: and A: markers. Here's the code that does this:

\documentclass{article}
\usepackage{enumitem}
\let\realitem\item% save the "real" \item command
\newcommand\questionansweritem{%
  \refstepcounter{enumi}
  \realitem[\ifodd\theenumi\textbf{Q:}\else\textbf{A:}\fi]
}
\newlist{interview}{enumerate}{1}
% make the interview list use \questionansweritem instead of \item
\setlist[interview]{before=\let\item\questionansweritem}

\begin{document}

  \begin{interview}
    \item This is the first question
    \item And this is the answer
    \item This is the second question
    \item Another answer
  \end{interview}

\end{document}

If you prefer to use \question and \answer, which is a good idea because this makes the code more readable, you can do this by adding the lines:

\let\answer\questionansweritem
\let\question\questionansweritem

The main advantage of using the enumitem package is that this makes it easy to change the spacing etc of your list of interview questions -- see the enumitem documentation for more details.

You can easily customise your list to allow, for example, the questions to be put in italics:

enter image description here

This was done using:

\documentclass{article}
\usepackage{enumitem}
\let\realitem\item% save the "real" \item command
\newcommand\questionansweritem{%
  \refstepcounter{enumi}
  % \item + turn on italics
  \ifodd\theenumi\realitem[\textbf{Q:}]\bgroup\itshape
  % \turn off italics + \item
  \else\egroup\realitem[\textbf{A:}]\fi
}
\newlist{interview}{enumerate}{1}
\setlist[interview]{before=\let\item\questionansweritem}
\let\answer\questionansweritem
\let\question\questionansweritem

\begin{document}

  \begin{interview}
    \question This is the first question
    \answer And this is the answer
    \question This is the second question
    \answer Another answer
  \end{interview}

\end{document}
1
  • Great answer, thanks. In fact, egreg's answer fulfill my needs at present, but I know myself and sooner or later I will undoubtedly try to further refine my environment, changing indentations, spacing, setting different styles for labels and list contents, etc., and your answer shows me some ways to explore. Commented Oct 7, 2018 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .