Spacing between groups of two list items
Spacing between groups of two list items
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
beginitemize
item[Q.] Question 1
item[A.] Answer 1
%space here
item[Q.] Question 2
item[A.] Answer 2
enditemize
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?
item

vspace<some amount>
vspace0.25cm
5 Answers
5
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.
question
answer
item[Q.]
item[A.]
question
0.25 cm

documentclassarticle
usepackagelipsum% Just for dummy text
makeatletter
newififfirstquestion
firstquestiontrue
defquestion%
iffirstquestion
firstquestionfalse
else
vspace0.25cm%
fi
item[Q.]
defanswer%
item[A.]
makeatother
begindocument
pagenumberinggobble
lipsum[1]
beginitemize
question Question 1
answer Answer 1
question Question 2
answer Answer 2
question Question 2
answer Answer 2
question Question 2
answer Answer 2
enditemize
lipsum[1]
enddocument
and, following A. Ellet's suggestion, if you will always label each item with "Question /Answer ", you can use a counter:
documentclassarticle
usepackagelipsum% Just for dummy text
makeatletter
newcounterQuestionCounter
newififfirstquestion
firstquestiontrue
defquestion%
iffirstquestion
setcounterQuestionCounter1% Remove this to have the same numbering across different itemize's
firstquestionfalse
else
vspace0.25cm%
fi
item[Q.] Question theQuestionCounter:space
defanswer%
item[A.] Answer theQuestionCounter:space
stepcounterQuestionCounter
makeatother
begindocument
pagenumberinggobble
lipsum[1]
beginitemize
question What's your name?
answer Foo
question Where do you live?
answer Bar
question How old are you?
answer -1
enditemize
lipsum[1]
enddocument
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
Sep 10 '18 at 23:13
paircounter
question
item[Q.] Question thepaircounter
answer
item[A.] Answer thepaircounter
@A.Ellett If the questions and answers will be labelled, then it's a good idea indeed. I added a version with that. Thanks :)
– Phelype Oleinik
Sep 10 '18 at 23:22
You have several options.
questionVSpace
These variants are implemented in the following MWE.
documentclassarticle
%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 vspace9ex with smallskip
newcommandquestionVSpacevspace9ex
newcounterquestionCounter
newcommandqAndA[2]
%The following code automatically adds the space unless it is the first question.
ifnumvaluequestionCounter>0
bigskip%you can choose a different space here.
fi
stepcounterquestionCounter
% You could also change "Q." to "Q. thequestionCounter"
item[Q.] #1
item[A.] #2
begindocument
sectionVariant A
Test
beginitemize
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
enditemize
sectionVariant B
Test
beginitemize
qAndA Text fooText bar
qAndA Text fooText bar
qAndA Text fooText bar
enditemize
enddocument

I think you forgot something at
ifthequestionCounter. This will always evaluate to false. See the extra vertical space between 0.2 B and Q. Text foo.– Phelype Oleinik
Sep 10 '18 at 23:14
ifthequestionCounter
0.2 B
Q. Text foo
@PhelypeOleinik Than you very much. I hope it is fixed now.
– CampanIgnis
Sep 10 '18 at 23:24
More simply, you could use a different itemize for each Q & A:
beginitemize
item[Q.] Question 1
item[A.] Answer 1
end[itemize}
beginitemize
item[Q.] Question 2
item[A.] Answer 2
enditemize
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.
QA
documentclassarticle
newcounteraeQAcounter
newcounteraePAIRcounter
defaeRememberLastPair1
newenvironmentQA[1]
%% save the definition of `item` so I can access
%% its definition later when I *redefine* it.
letaeitemitem
%% a crude key value'ish approach to remembering
%% previous value for question/answer pair.
defaeTestA#1%%
defaeTestBremember previous%%
ifxaeTestAaeTestB
%% nothing to do here!!
else
defaeRememberLastPair1%%
fi
setcounteraePAIRcounteraeRememberLastPair%%
beginlist%%
sffamily
ifoddtheaeQAcounter Question theaePAIRcounter%%
else
Answer theaePAIRcounter%%
stepcounteraePAIRcounter%%
fi
:%%
usecounteraeQAcounter%%
defitem%%
%% only add space if not the first question
ifnumtheaePAIRcounter=aeRememberLastPair
%% do nothing!!!!
else
%% only add space before question and after
%% an answer (the even `item` in the list)
ifoddtheaeQAcounter
else
vspace4ex%%
fi
fi
aeitem
letquestionitem
letansweritem
xdefaeRememberLastPairtheaePAIRcounter%%
endlist
begindocument
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabitur dictum gravida mauris. Nam arcu
beginQA
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.
endQA
Nam dui ligula, fringilla a, euismod sodales, sollicitudin
vel, wisi. Morbi auctor lorem non justo. Nam lacus libero,
pretium at, lobortis vitae, ultricies et.
textbfsffamily And, we continue with more questions:
beginQA[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
endQA
textbfsffamily And, Here is a new set of questions:
beginQA
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
endQA
enddocument

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.
question
answer
item
item
list
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).
vspace...
list
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.
itemsep
itemsep
vspace
bigskip
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:
item
documentclassarticle
newcommandQ[1]item[bfseries Q.]
itemsep2pt plus 2pt #1
newcommandA[1]item[bfseries A.]
itemsep1em plus .2em minus .1em #1
newenvironmentq-asection*Questions
and answersbeginitemizeenditemize
begindocument
beginq-a
Q Question 1
A Answer 1
Q Question 2
A Answer 2
Q Question 3
A Answer 3
endq-a
enddocument

Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to TeX.SX! A hackish way would be to insert
vspace<some amount>there. For example,vspace0.25cm. This would be quite boring for longer lists though...– Phelype Oleinik
Sep 10 '18 at 22:48