5

I'm trying to format a homework document, in which I want to write the questions and answers in a list. The question is followed by an answer, after which I want a slightly bigger gap before the next question and answer

\begin{itemize}
    \item[Q.] Question 1
    \item[A.] Answer 1
    %space here
    \item[Q.] Question 2
    \item[A.] Answer 2
\end{itemize}

Currently, I'm adding a space by using an empty list item (\item[]). The output is shown below. Unfortunately, this creates a much larger space than I want. Is there a cleaner way to do this?

Output

1
  • Welcome to TeX.SX! A hackish way would be to insert \vspace{<some amount>} there. For example, \vspace{0.25cm}. This would be quite boring for longer lists though... Commented Sep 10, 2018 at 22:48

5 Answers 5

6

Very basic solution which doesn't require manual spacing:

Define two macros, \question and \answer, which do \item[Q.] and \item[A.], respectively. The \question macro also inserts a vertical space of 0.25 cm if it's not in the first question, so the space between the list and the paragraph is right.

enter image description here

\documentclass{article}

\usepackage{lipsum}% Just for dummy text

\makeatletter
\newif\iffirstquestion
        \firstquestiontrue
\def\question{%
  \iffirstquestion
    \firstquestionfalse
  \else
    \vspace{0.25cm}%
  \fi
  \item[Q.]}
\def\answer{%
  \item[A.]}
\makeatother

\begin{document}

\lipsum[1]

\begin{itemize}
    \question Question 1
    \answer Answer 1
    \question Question 2
    \answer Answer 2
    \question Question 2
    \answer Answer 2
    \question Question 2
    \answer Answer 2
\end{itemize}

\lipsum[1]

\end{document}

and, following A. Ellet's suggestion, if you will always label each item with "Question /Answer ", you can use a counter:

\documentclass{article}

\usepackage{lipsum}% Just for dummy text

\makeatletter
\newcounter{QuestionCounter}
\newif\iffirstquestion
        \firstquestiontrue
\def\question{%
  \iffirstquestion
    \setcounter{QuestionCounter}{1}% Remove this to have the same numbering across different itemize's
    \firstquestionfalse
  \else
    \vspace{0.25cm}%
  \fi
  \item[Q.] Question \theQuestionCounter:\space}
\def\answer{%
  \item[A.] Answer \theQuestionCounter:\space
  \stepcounter{QuestionCounter}}
\makeatother

\begin{document}

\lipsum[1]

\begin{itemize}
    \question What's your name?
    \answer Foo
    \question Where do you live?
    \answer Bar
    \question How old are you?
    \answer -1
\end{itemize}

\lipsum[1]

\end{document}
2
  • I would incorporate a counter, paircounter for each question/answer pair so that \question expands to \item[Q.] Question \thepaircounter and \answer expands to \item[A.] Answer \thepaircounter.
    – A.Ellett
    Commented Sep 10, 2018 at 23:13
  • @A.Ellett If the questions and answers will be labelled, then it's a good idea indeed. I added a version with that. Thanks :) Commented Sep 10, 2018 at 23:22
3

More simply, you could use a different itemize for each Q & A:

\begin{itemize}
    \item[Q.] Question 1
    \item[A.] Answer 1
\end[itemize}

\begin{itemize}
    \item[Q.] Question 2
    \item[A.] Answer 2
\end{itemize}
3

You have several options.

  • The simplest is: Define your own command, say \questionVSpace, to use a space between elements within a list. Observe that the definition of vertical space command may depend on the class you use.
  • There are more elaborate ways to do it. I recommend using a command for doing so. This command should handle the vertical space automatically and should handle layout issues globally, e.g. if you want to change "Q." to "Question". A crude but more useful way is my second variant. This also allows to enumerate the question, see my code below.

These variants are implemented in the following MWE.

\documentclass{article} 

%you can manually choose a space here if you do not like the predefined ones
%In case you want to use one of the predfined ones, say \smallskip, you should still use \questionVSpace and replace \vspace{9ex} with \smallskip
\newcommand{\questionVSpace}{\vspace{9ex}}


\newcounter{questionCounter}
\newcommand{\qAndA}[2]{
%The following code automatically adds the space unless it is the first question. 
\ifnum\value{questionCounter}>0
\bigskip%you can choose a different space here.
\fi
\stepcounter{questionCounter}
% You could also change "Q." to "Q. \thequestionCounter"
\item[Q.] #1 
\item[A.] #2
}

\begin{document}
\section{Variant A}
Test
\begin{itemize}
    \item[Q.] Question 1
    \item[A.] Answer 1
    \smallskip
    \item[Q.] Question 2
    \item[A.] Answer 2
    \medskip
    \item[Q.] Question 3
    \item[A.] Answer 3
    \bigskip
    \item[Q.] Question 4
    \item[A.] Answer 4
    \questionVSpace
    \item[Q.] Question 5
    \item[A.] Answer 5
\end{itemize}

\section{Variant B}
Test
\begin{itemize}
    \qAndA {Text foo}{Text bar}
    \qAndA {Text foo}{Text bar}
    \qAndA {Text foo}{Text bar}
\end{itemize}
\end{document}

enter image description here

2
  • 1
    I think you forgot something at \if\thequestionCounter. This will always evaluate to false. See the extra vertical space between 0.2 B and Q. Text foo. Commented Sep 10, 2018 at 23:14
  • @PhelypeOleinik Than you very much. I hope it is fixed now. Commented Sep 10, 2018 at 23:24
2

Here's an approach using a new environment called QA. I've tried to throw in enough to let you see what's possible beyond just what you asked and to allow you to see where additional tweaks might enhance what you're doing. I've exaggerated the effect of adding vertical space between an answer and a following question: tweak to something more suitable.

\documentclass{article}

\newcounter{aeQAcounter}
\newcounter{aePAIRcounter}
\def\aeRememberLastPair{1}
\newenvironment{QA}[1][]
  {%% save the definition of `\item` so I can access 
   %% its definition later when I *redefine* it.     
    \let\aeitem\item
   %% a crude key value'ish approach to remembering  
   %% previous value for question/answer pair.       
    \def\aeTestA{#1}%%
    \def\aeTestB{remember previous}%%
    \ifx\aeTestA\aeTestB
      %% nothing to do here!!
    \else
      \def\aeRememberLastPair{1}%%
    \fi
    \setcounter{aePAIRcounter}{\aeRememberLastPair}%%
    \begin{list}{%%
      \sffamily
        \ifodd\theaeQAcounter Question \theaePAIRcounter%%
        \else
          Answer \theaePAIRcounter%%
          \stepcounter{aePAIRcounter}%%
        \fi
      :%%
      }
      {\usecounter{aeQAcounter}}%%
    \def\item{%%
      %% only add space if not the first question
      \ifnum\theaePAIRcounter=\aeRememberLastPair
        %% do nothing!!!!
      \else
        %% only add space before question and after
        %% an answer (the even `item` in the list)
        \ifodd\theaeQAcounter
        \else
          \vspace{4ex}%%
        \fi
      \fi
      \aeitem}
    \let\question\item
    \let\answer\item
  }
  {\xdef\aeRememberLastPair{\theaePAIRcounter}%%
   \end{list}}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabitur dictum gravida mauris. Nam arcu
\begin{QA}
\question What do you say when you part from someone?
\question bye
\question What is a fruit that shouldn't be mixed with apples?
\answer   An orange.
\end{QA}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin
vel, wisi. Morbi auctor lorem non justo. Nam lacus libero,
pretium at, lobortis vitae, ultricies et.
\textbf{\sffamily And, we continue with more questions:}
\begin{QA}[remember previous]
\question Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
\answer   Cras nec ante. Pellentesque a nulla.  Cum sociis natoque penatib
\question Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
\answer   Cras nec ante. Pellentesque a nulla.  Cum sociis natoque penatib
\end{QA}

\textbf{\sffamily And, Here is a new set of questions:}
\begin{QA}
\question Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
\answer   Cras nec ante. Pellentesque a nulla.  Cum sociis natoque penatib
\question Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
\answer   Cras nec ante. Pellentesque a nulla.  Cum sociis natoque penatib
\end{QA}


\end{document}

enter image description here

Notice that I use two counters here. One counter is essentially the list's counter for each item. If it's odd, that means I'm asking a question. If it's even, that means I'm answering a question. The second counter is only advanced once the question/answer pair has been established.

Also, notice that \question and \answer are stupid, in the sense that they're really just stand-ins for \item and don't do much else. Everything is handled through \item and tracking counters. I try to let the environment itself handle the tracking of questions and answers. The only difficulty comes when adding space. From within the {list} environment's arguments, not much can be done.

One thing to note, you cannot insert vspace{...} from within the first argument to the {list} environment. It will do nothing there and just be ignored for the most part (or show up where you don't expect it).

2

The space between items is controlled by \itemsep, so you can fix for each item, with the advantage that your Q & A lists will not influenced by the default \itemsep as it would happen using some additional vertical space as \vspace,\bigskip. I will add some glue too, not a fixed length, that would help to avoid orphan items/lines.

This can be done easily as already proposed making a macro for each item type, and I think that semantically is better that a complex redefine of \item type to hiddenly switch from "Q." to "A.". With the "keep it simple" principle in mind, I do not see the need of conditionals or counters (I will edit the answer if you clarify this point), so the macros could be some like this:

\documentclass{article}

\newcommand\Q[1]{\item[\bfseries Q.]
\itemsep2pt plus 2pt #1}
\newcommand\A[1]{\item[\bfseries A.]
\itemsep1em plus .2em minus .1em #1}

\newenvironment{q-a}{\section*{Questions 
and answers}\begin{itemize}}{\end{itemize}}

\begin{document}

\begin{q-a}
    \Q Question 1
    \A Answer 1 
    \Q Question 2
    \A Answer 2 
    \Q Question 3
    \A Answer 3 
\end{q-a}

\end{document}

mwe

You must log in to answer this question.

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