Troubleshooters.Com, T.C Linux Library and Litt's LyX Library Present

Making Your Own Lists
in LaTeX and LyX

Copyright (C) 2007 by Steve Litt, All rights reserved. Material provided as-is, use at your own risk.


Contents

Why You Need Your Own Lists

LyX provides excellent list environments, including itemize, enumerate and description. If those don't fit your needs you can usually use a package to do what you need. But once in a while, you can't find a pre-designed list fitting your needs. Then you must build your own list environments and put them in a layout file in order to use them in LyX.

I recently had just such a need. I needed to make chapter quizzes in a book. Each quiz consists of a numbered list of questions. Each question is followed by a lettered list of possible answers. One of the answers is correct, and depending on the situation the correct answer would need to be identified. In order to easily use this in LyX, all of this must be expressed as environments.

Two existing packages might have fit the bill: exam.cls (http://www-math.mit.edu/~psh/) or examdesign.cls
(http://tug.ctan.org/pkg/examdesign/). However, each of those was a document class custom made for making an exam to hand out to students. To use them in my book, I would have had to use them as a document class, which of course is silly. I also could have stripped out all the exam specific stuff and put the remainder in a layout, but by that time it's easier to just to build your own from scratch.

This document is primarily LaTeX. The linking of LaTeX environments and commands to LyX is well documented elsewhere on this website. When you need to make your own lists, use this document as a starting point.

List Hello World

The following manually implements a very simple list. The "label" is nothing but an asterisk bullet.

\documentclass[12]{article}
\begin{document}
Hello world
\begin{list}{*}{}
\item Item one
\item Item two
\item Item three
\end{list}
\end{document}
Standard for any LaTeX doc
Standard for any LaTeX doc
Arbitrary normal text
Define the list properties and start the list
List item
List item
List item
End the list
Standard for any LaTeX doc

The result, cropped to just the text and without headers and footers, looks like this:

Hello World output

Explanation

A list consists of a series of items, each starting on a new line. Each item consists of a label and a body. The label is a bullet or a number or letter, or some text, any of which can be formatted with specific fonts or appearences. Here's how a list is created manually:

\begin{list}{label code}{body code}
\item first item
\item second item
\item third item
\end{list}

The list declaration/definition looks like this:
\begin{list}{label code}{body code}
That's it -- just \begin followed by three brace enclosed entities:
The label code is all code used to create the bullet or the numbering or whatever delineates a new list item. It can contain font properties, typefaces, the width of the label, and/or a counter to be displayed with commands like \alph, \Alph, \roman, \arabic and the like.

Notice that the body code sometimes contains code you might expect to be in the label, such as \usecounter{}.

As you can see from the previous pseudocode, everything between the declaration/definition and the \end{list} is list items, starting with \item.

Converting Manual Lists to Environments

If you use the same type of list over and over, you don't want to keep typing its definition. Also, if you're using LyX, you want to work with environments, not ERT (Evil Red Text -- in other words, inline LaTeX within the LyX). So you need to convert the manual list code to environment code capable of being placed in a layout file. To minimize complexity, let's do that with the Hello World code so you can see it isn't rocket science. If you remember, this is the Hello World code, featuring a completely manual declaration/definition/item structure:

\documentclass[12]{article}
\begin{document}
Hello world
\begin{list}{*}{}
\item Item one
\item Item two
\item Item three
\end{list}
\end{document}

And once again, the \begin statement for a list looks like this:
\begin{list}{label code}{body code}
Remember that an environment declaration/definition looks like this:
\newenvironment{environment_name}{initialization code}{finalization code}
Let's duplicate that, but put it on multiple lines:
\newenvironment{environment_name}{
initialization code
}{
finalization code
}
The initialization code is the declaration/definition of the list, and the finalization code ends the list. At this point we'll just use the \item command for list items. We'll call our new list environment questions. Let's put the list inside the environment, replacing the initialization and finalization code:
\documentclass[12]{article}

\newenvironment{questions}{
\begin{list}{*}{}
}{
\end{list}
}

\begin{document}
Hello world
\begin{questions}
\item Item one
\item Item two
\item Item three
\end{questions}
\end{document}
In the preceding, we've substituted the \begin{list}{*}{} for the environment's initialization code, and the \end{list} for the environment's finalization code. We've placed them in the document preamble (before the \begin{document}) and refer to the defined questions environment from the document.

Please study the preceding until you understand it. It's simple substitution, but you need to understand it in this simple example, because in real life complex examples, the nested parentheses can drive you nuts. Only if you understand that what you're really doing is substitution can you figure your way into reading, writing and debugging complex lists.

The preceding code gets us most of the way to LyX heaven, but we still have the \item commands which would necessitate ERT within LyX. That would be unacceptable. So what we'll do is create an environment called question (note that this is singular) to represent the list items. Here's the code:

\documentclass[12]{article}

\newenvironment{questions}{
\begin{list}{*}{}
}{
\end{list}
}

\newenvironment{question}{\item}{}

\begin{document}
Hello world
\begin{questions}

\begin{question}Item one\end{question}
\begin{question}Item two\end{question}
\begin{question}Item three\end{question}

\end{questions}
\end{document}

Wrapup: Hello World

This article has presented the most trivial possible home-made list. Its label was nothing more than an asterisk. It had no formatting of any kind.

The reason it was made so simple is to make understanding it easy, so that when confronted with much more complex lists, or the need to make them, you can break things down to these simple components.

The last part of this article framed the list as environments. The next several articles will go back to manually constructed lists to emphasize the points being made and avoid needless nesting of braces. Much later on this web page you will be presented with a fully environmentalized environment for listing questions and answers, very similar to the questions at the end of each chapter in a textbook. For simplicity however, the next few articles will leave environments behind.

Numbered Lists

A numbered list is just a list whose label contains a number. To assume any meaning, that number must be a counter that is declared in the document preamble. Let's start with the original Hello World:

\documentclass[12]{article}
\begin{document}
Hello world
\begin{list}{*}{}
\item Item one
\item Item two
\item Item three
\end{list}
\end{document}

Now let's give it numbers:

\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
Hello world
\begin{list}{Question \arabic{qcounter}:~}{\usecounter{qcounter}}
\item Item one
\item Item two
\item Item three
\end{list}
\end{document}

What we did was, in the document preamble (the area before \begin{document} we defined a counter called qcounter, and then we changed the definition of the list. Specifically, we added the word "Question" to the label, we replaced the asterisk with an Arabic numeral presentation of qcounter, we added \usecounter{qcounter} within the body code of the list to tell the list to print and increment qcounter.

The placement of \usecounter{qcounter} is one of those places where you might be surprised at its placement. After all, it's used in the label of each item, not the body of each item. Nevertheless, the body code is where you put it.

The preceding code produces output that looks like this (after significant cropping):

A simple numbered list

You might notice that the items start to the left of the "Hello World" instead of the right. You can space items any way you like. Item spacing has its own article.

Numbers can be shown as Arabic numerals, upper case roman numerals, lowercase Roman numerals, uppercase letters A-Z or lowercase letters a-z:
Command       What it does
\arabic{qcounter}
Print qcounter with Arabic numerals
\Roman{qcounter}
Print qcounter with Uppercase Roman numerals
\roman{qcounter}
Print qcounter with Lowercase Roman numerals
\Alph{qcounter}
Print qcounter with Uppercase letters (A->1, Z->26)
\alph{qcounter}
Print qcounter with Lowercase letters

Spacing List Items

If it weren't for spacing concerns, you wouldn't even need lists. You could just make a simple environment that prints the equivalent of a label, skips a half inch, and prints the equivalent of the body. Trouble is, if the body wraps, it wraps to below the label equivalent.

To get an instantly readable list, you need it spaced correctly. The labels must stick out to the left. In my opinion, the bodies should all left align, whether they're single line or wrapped, or even separate paragraphs (you can have a multi-paragraph list item). Inter item spacing should exceed inter paragraph spacing, which itself should exceed inter line spacing. Label width should be large enough to handle the longest label, so everything lines up no matter how many digits the counter (and remember the Roman numeral for 8 has four characters). To get an instantly readable list, you need it spaced correctly.

Here are some of the common vertical spacing lengths used with list items:
Command What it does
\parskip
Space between paragraphs outside of a list, and part of the space between a non-list paragraph and a list item.
\topsep
Extra space added to \parskip before the first and after the last item.
\parsep
Paragraph separation within a single item.
\itemsep
Extra inter-item spacing added to \parsep.
\partopsep
This is added to the top and/or bottom of the list if and only if there's a blank line above or below the first or last item. Leave this alone unless blank lines become a problem.

Here are some of the common horizontal spacing lengths used with list items:
Command What it does
\leftmargin
Distance from the left edge of the current environment to the left margin of the item label. Remember, environments can nest. Defaults to 0.
\rightmargin
Distance from the right edge of the current environment to the right margin of the item body. Defaults to 0.
\labelwidth
Width allotted to the label. This should be set at least to or more than the longest expected label.
\labelsep
The distance between the rightmost part of the label (assuming you haven't changed the label from its default right justification) to the left margin of the item body. This is one of the handiest adjustments you can make to create the ultimately readable list for your exact situation. Use it early and often.

BEWARE: This setting enforces this distance by shoving the label left rather than moving the body left margin right. If you set this you might need to add a corresponding amount to \leftmargin, if you want your labels in a specific place.
\listparindent
The indent of the first line of each paragraph in an item, except for the first paragraph of an item. It can be a little ugly, but if you're pressed for vertical space and want to decrease interparagraph spacing within items while still giving the user cues as where new paragraphs begin, this is the way to do it.
\itemindent
This length is capable of causing some real ugliness -- leave it alone unless you have a really good reason not to. What this horrid adjustment does is takes the label and first line of a multiline body, and push them left from the normal item body left margin. This makes the body lines not line up. It's ugly. If you already have a list where multiline items look wrong, try setting this length to 0 to see whether a previous global setting of this length has caused problems.

Don't set this length except out of self-defense. It's trouble.

WARNING

Except for \parskip, which isn't a list property, all these lengths should be set in the list's body code:
\begin{list}{*}{\setlength\itemsep{0.2in}}
It's sometimes possible to do it elsewhere, but it's best to do it in the list's body code.

Spacing Examples

This section contains various spacing examples. Each example's label has the word "Question" and then the number. Each item is bold font to make it easier to see at low magnifications. In each example, item number four is the first two paragraphs of Lincoln's Gettysburg Address.
Default Spacings
Default spacing
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

The preceding is default spacing. It has no \setlength commands. Note the ugliness of question 4, where the text wraps to the left of the end of the label. This is caused by the \labelwidth length being less than the width of the label. Let's correct that by specifying a labelwidth of 3 inches, which is clearly more than is needed:
\setlength\labelwidth{3in}
\setlength\labelwidth{3in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

Ahh, that's better. The body for question 4 lines up as a block, because \labelwidth is set larger than the longest label. The labels have all been pushed to the left by the length that wrapped lines were to the left of the first line in the default. The amount by which \labelwidth exceeded the longest label does not effect how far labels were pushed to the left, nor anything else. The only effect of an extra large \labelwidth is that it can accommodate extra large labels, should such a need arise. Otherwise it has no effect. Thus, the preceding screenshot would have looked the same whether \labelwidth had been set to 1", 3" or 7".

Because this nice block effect is the effect we want to achieve, all subsequent screenshots will include a 3" \labelwidth.

Many times you want to change the distance between labels and item bodies. This is done with the \labelsep length. As discussed earlier, this adjustment works by shoving the labels left, not by soving the bodies right. Let's set \labelsep to 2in. This is probably too much to be practical in normal circumstances, but it makes the effect obvious:
\setlength\labelwidth{1in}
\setlength\labelsep{1in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\labelsep{1in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

In the preceding, you see that the item bodies are exactly where they were before, under the "e" of Hello. However, the labels were pushed left one inch. Sometimes that's what you want, but sometimes you wanted the labels to stay put, and simply move the left margin of the bodies to the right. If that's the case, what you need to do is move the left margin of the items to the right, which moves the labels to the right and moves the bodies' left margin to the right also.

So, in the case of the preceding \labelsep adjustment, if you want the label to stay put, you must add an amount to the \leftmargin length. The following shows a one inch readjustment to attempt to keep the long label separation, but move the labels back to their original position:
\setlength\leftmargin{1in}
\setlength\leftmargin{1in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\labelsep{1in}
\setlength\leftmargin{1in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

In the preceding, everything except the right margin was pushed to the right, and the item bodies wrapped tighter to  accommodate the larger left margin.  HOWEVER, the labels were not moved far enough to the left to compensate for the 1 inch \labelsep. Let's try 1.27 inches:
\setlength\leftmargin{1.27in}
\setlength\leftmargin{1.27in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\labelsep{1in}
\setlength\leftmargin{1.27in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

The preceding gets the labels just about where they were before, with the colon ever so slightly to the left of the "H" in "Hello". Now let's indent paragraphs, other than the first paragraph, in items, by setting \listparindent to one inch:
\setlength\listparindent{1in}
\setlength\listparindent{1in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\labelsep{1in}
\setlength\leftmargin{1.27in}
\setlength\listparindent{1in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

In the preceding, the only difference from the one before it is that the first line of the second paragraph is indented one inch. This makes it easier to discern individual paragraphs within an item. Obviously, one inch is too much -- a half an inch would be better for most applications.

Next, let's really garbage up this document by setting  \itemindent to one inch:
\setlength\itemindent{1in}
\setlength\itemindent{1in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\labelsep{1in}
\setlength\leftmargin{1.27in}
\setlength\listparindent{1in}
\setlength\itemindent{1in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

OK, that's just plain wrong! The wrapped lines are shoved an inch left of the nonwrapped items. Meanwhile, the entire left margin is shoved right. This is truly ugly. Just say no to \itemindent!

The one exception is if you're already getting symptoms similar to the screenshot above. In that case, try setting \itemindent to 0 inches, and see if the symptom goes away. Possibly something global set this length to a nonzero value, screwing up your wrapped lists.

Adjusting Vertical Spacing

Sometimes you want lots of space between list items, sometimes you want just a little. Sometimes you know beforehand that no items will wrap, and sometimes items will wrap. When you know nothing will wrap, you can pretty much just increase or decrease \itemsep. Things get much more complex when items can wrap, and especially when it's possible for an item to contain multiple paragraphs.

The purpose of vertical spacing is clarity. What you want to do is set items as close together as possible while retaining the desired level of clarity. To really emphasize something, you might choose to put four items on a page. Usually you'll try to make items tighter, especially because the looser the item spacing, the more likely a list will span pages, which is confusing for the reader.

In general, the following principle should always be true:

interline spacing < interparagraph spacing < interitem spacing

If you use standard line spacing with wrapping items, or especially wrapping multi-paragraph items, the preceding principle would require a fairly loose spacing. It's often a good idea to use the setspace package to shrink line spacing within items in order to reduce the needed paragraph and item separations. Also, you can set paragraph separation to 0 by indenting first lines of paragraphs other than the first one (\setlength\listparindent{1in}). Let's explore with a few examples, starting with default vertical spacing:
Horizontal default
Horizontal default
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}
The preceding looks pretty good, with two possible exceptions:
  1. The interparagraph spacing in question 4 is a little too small to easily delineate the paragraphs.
  2. This list is pretty spread out, which can consume a lot of page surface on long lists.
One way to fix both these problems is to indent non-first paragraphs in each item, and to shrink \itemsep:
\setlength\listparindent{0.5in}
\setlength\parsep{0in}
\setlength\parsep{0in}
\documentclass[12]{article}
\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setlength\listparindent{0.5in}
\setlength\parsep{0in}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

In the preceding, you indented paragraphs, thus eliminating the need for any extra spacing between paragraphs. So you also set \parsep to 0 inches, thereby making interparagraph spacing the same as interline spacing, and also reducing spacing between items, because items are separated by \parsep plus \itemsep.

Now, let's instead condense this without indentation, but instead reducing interline spacing so that paragraph and item spacing can also be reduced:
\setstretch{0.8}
\documentclass[12]{article}
\usepackage{setspace}

\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setstretch{0.8}
\setlength\parsep{1ex}
\setlength\itemsep{1ex}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

The preceding uses the setspace package (\usepackage{setspace}) to condense line spacing via the \setstretch{0.8} command. Now that line spacing is condensed, \parsep can be set to the tiny value of 1 ex, and \itemsep can likewise be set to 1 ex, so that true interitem spacing will be the sum of the two. The result is a pleasing list where items have significant  spacing, paragraph transitions are recognizeable, and the list is fairly short.

You could make it even shorter by setting \setlength\listparindent{0.5in} and \setlength\parsep{0in}:
\setstretch{0.8}
\setlength\listparindent{0.5in}
\setlength\parsep{0ex}
\setlength\itemsep{1ex}
Minimum vertical spacing
\documentclass[12]{article}
\usepackage{setspace}

\newcounter{qcounter}
\begin{document}
\textbf{Hello world}
\begin{list}
{\bfseries{}Question \arabic{qcounter}:~}
{
\usecounter{qcounter}
\bfseries
\setlength\labelwidth{3in}
\setstretch{0.8}
\setlength\listparindent{0.5in}
\setlength\parsep{0ex}
\setlength\itemsep{1ex}
}
\item Item one
\item Item two
\item Item three
\item Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any
nation, so conceived and so dedicated, can long endure. We are met on a great
battle-field of that war. We have come to dedicate a portion of that field, as
a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
\item Item five
\end{list}
\end{document}

The preceding is a little too vertically compressed for my taste, but if you have long lists it might be necessary.

Summary

This article discussed the basics of list-related spacing, providing detailed examples of some of the more common settings. When making your own lists, use this chapter as a reference for spacing settings.

Multiple Choice Quizzes: Conceptual Discussion

Quizzes consist of a numbered list of questions, and following each question, a lettered list of possible answers, one of which is correct. The possible answers (let's call them choices from now on) should be indented from the questions. Both questions and answers can be expected to wordwrap on occasion, and it's conceivable they'll consist of multiple paragraphs, especially questions. The choice letters start over again at "A" for each question.

The LyX or LaTeX source file should signify which choice is the right one, but of course this signification should not be shown to the student. However, it should be available to create an answer key.

Since it's intended to work with LyX, it's vital that everything is implemented as environments. The way I've visualized it is to have five environments:
Questions defines the list of questions, while Question invokes one question item. Similarly, Choices defines the list of choices, while Choice invokes one choice item. Rightchoice also invokes a choice item, but has code so that if variable \showcorrect is set to true , the Rightchoice environment will print the choice dark and with leading and trailing asterisks. Questions uses the \qcounter counter and Choices uses the \acounter counter, which is set on the counter level to reset when \qcounter changes.

So what you have is a list of questions, each of which has its own indented list of possible answers, and within the LaTeX source code, one of the answers to each question is marked as correct by use of the Rightchoice environment. The \showcorrect variable, which can be changed on the fly, enables Rightchoice to print dark and with surrounding asterisks if set to true.

This is not intended to be a professional implementation of a quiz, but it certainly gets the point across. This solution's implementation is shown in the next article.

Implementation of the Multiple Choice Quiz

A quiz has several questions, each with several possible choices. One of each question's choices is correct. Here's a quiz I produced with my own LaTeX:

Output of Multiple Choice Quiz
Output of quiz LaTeX
This is the quiz, printed after some plain text (Hello world). The questions are numbered, and each question's possible choices are lettered. The possible choices are indented from the question above them, and each question has a significant space between it and what came above it. The result is an obviously readable multiple choice test.

In each question, the correct response is bold, italicized, and is preceded and followed by a couple of stars. The ability to identify the correct answer must be flagged so it can be turned on or off. Obviously the students mustn't see the correct answers.


LaTeX Implementation
of Multiple Choice Quiz
\documentclass[12,lettersize]{article}

%%%% PACKAGE FOR MARGINS %%%%
\usepackage[letterpaper,left=1in,right=1in,top=1in,bottom=1in]{geometry}

%%%% PACKAGE FOR SETTING LINE SPACING %%%%
\usepackage{setspace}

%%%% IFTHEN PACKAGE FOR FIGURING WHETHER CORRECT ANSWERS LOOK DIFFERENT %%%%
\usepackage{ifthen}

%%%% SHOW CORRECT ANSWER??? %%%%
\def\showcorrect{true}
%\def\showcorrect{false}

%%%% QUESTION COUNTER %%%%
\newcounter{qcounter}

%%%% ANSWER COUNTER RESETS ON CHANGE OF QUESTION COUNTER %%%%
\newcounter{acounter}[qcounter]

%%%% MARKER (DOUBLE ASTERISK) FOR FRONT OF CORRECT ANSWER %%%%
\def\correctmarker{init}

%%%% ENVIRONMENT FOR LIST FOR QUESTIONS LIST %%%%
\newenvironment{questions}{ % %%%% Begin preliminary environment code
\begin{list}{ % %%%% Begin list item label code
\bfseries\upshape\arabic{qcounter}:
}{ % %%%% Begin list item body code
\usecounter{qcounter}
\setlength{\labelwidth}{1in}
\setlength{\leftmargin}{0.25in}
\setlength{\labelsep}{0.5ex}
\setlength{\itemsep}{2em}
} %%%%% End list item body code
}{ %%%%% Begin wrapup environment code
\end{list}
} %%%%% End wrapup environment code

%%%% ENVIRONMENT FOR A SINGLE QUESTION %%%%
\newenvironment{question}{\item{}}{}

%%%% ENVIRONMENT FOR THE LIST OF CHOICES FOR A QUESTION %%%%
\newenvironment{choices}{ %%%%% begin preliminary environment code
\begin{list}{ %%%%% Begin list item label code
\bfseries\upshape
\correctmarker\alph{acounter}:
}{ %%%%% begin list item body code
\usecounter{acounter}
\setlength{\topsep}{-0.3ex}
\setlength{\labelwidth}{1in}
\setlength{\leftmargin}{0.7in}
\setlength{\labelsep}{0.5ex}
\setlength{\rightmargin}{0.5in}
\setlength{\itemsep}{1ex}
\setlength{\parsep}{0ex}
\setstretch{0.8}
\setlength{\listparindent}{0.5in}
} %%%%% end list item body code
}{
\end{list}
}

%%%% ENVIRONMENTS FOR A SINGLE WRONG OR A SINGLE CORRECT CHOICE %%%%
\newenvironment{choice}{\def\correctmarker{~~~~}\item{}}{}
\newenvironment{correctchoice}{
\ifthenelse{\equal{\showcorrect}{true}}{
\def\correctmarker{** ~}
\item\bfseries\slshape
}{
\def\correctmarker{~~~~}
\item{}
}

}{
\ifthenelse{\equal{\showcorrect}{true}}{%
~~**
}{%
}
}


%%%% THE DOCUMENT ITSELF %%%%
\begin{document}
Hello world
~\\[0.2in]
\begin{questions}

\begin{question}What is LyX?\end{question}
\begin{choices}
\begin{choice}A wordprocessor.\end{choice}
\begin{choice}A text editor.\end{choice}
\begin{choice}Four score and seven years ago our fathers brought forth on
this continent a new nation, conceived in Liberty, and dedicated to the
proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or
any nation, so conceived and so dedicated, can long endure. We are met on a
great battle-field of that war. We have come to dedicate a portion of that
field, as a final resting place for those who here gave their lives that
that nation might live. It is altogether fitting and proper that we should
do this.
.\end{choice}
\begin{correctchoice}A layout and typesetting program.\end{correctchoice}
\begin{choice}A seamless, business rule aware middleware product.\end{choice}
\end{choices}

\begin{question}Why is LyX better?\end{question}
\begin{choices}
\begin{choice}It's a Microsoft program.\end{choice}
\begin{correctchoice}It uses LaTeX to create superior layout.\end{correctchoice}
\begin{choice}Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in Liberty, and dedicated to the proposition
that all men are created equal.\end{choice}
\begin{choice}It's a fixed sequence, rapid authoring environment.\end{choice}
\begin{choice}You needn't use styles with LyX.\end{choice}
\end{choices}

\begin{question}What is LyX's relationship to LaTeX\end{question}
\begin{choices}
\begin{correctchoice}LyX is a front end to LaTeX.\end{correctchoice}
\begin{choice}LaTeX is a front end to LyX.\end{choice}
\begin{choice}Now we are engaged in a great civil war, testing whether that
nation, or any nation, so conceived and so dedicated, can long endure. We
are met on a great battle-field of that war. We have come to dedicate a
portion of that field, as a final resting place for those who here gave
their lives that that nation might live. It is altogether fitting and
proper that we should do this.
\end{choice}
\begin{choice}They're dedicated life partners.\end{choice}
\begin{choice}LaTeX is a wordprocessor with Visual Basic for
Applications, Object Embedding and Linking, and TrueType fonts.\end{choice}
\begin{choice}LyX is a corporation.\end{choice}
\end{choices}

\end{questions}

\end{document}
To the left is the LaTeX producing the preceding output. This LaTeX uses three external packages: geometry to specify margins, setspace to shrink line spacing, and ifthen to facilitate if/then logic in order to turn on or turn off identification of the correct choice.

The flag this file uses to determine whether or not to identify the correct answer (with asterisks, italics and bold) is \showcorrect.

Two counters are used: qcounter for questions, and acounter for answers. The \newcounter statement for acounter relates it to qcounter in such a fashion that when qcounter changes, acounter is reset back to 1 (or its letter equivalent, a).

The \correctmarker command, which is used as a string, is declared. This will later be used to put the double asterisk within the correct choice's label.

The questions list environment uses bold labels with arabic representations of counter qcounter.  It uses counter qcounter, and within the body portion sets various measurements. At 2em, the \itemsep length is quite long, so as to put plenty of space above a question and make it clear what question owns what choices.

The question environment prints a list item.

The choices list environment lists choices for a question, using a lower case alphabetic representation of the acounter counter. These choices are indented a half inch to the right of their questions (\setlength{\rightmargin}{0.5in}). Also, \topsep is set to -0.3ex to move the choices right up below their question.

The choices list must be vertically compact, so line spacing is shrunk to 80% (\setstretch{0.8}), paragraph spacing is set to 0 (\setlength{parsep}{0ex}) and to replace spacing as paragraph identification, paragraphs are indented (\setlength{\listparindent}{0.5in}). The \itemsep length is set to the tiny value of 1ex, whihc is the height of a lower case s, to reduce spacing between items.

The choice environment sets \correctmarker to four nonbreaking spaces and then prints the item. The correctchoice environment checks the \showcorrect flag and if it's set to true, sets \correctmarker to two asterisks, a space and a nonbreaking space. The finalization part of correctchoice also prints two nonbreaking spaces and two asterisks at the end. If \showcorrect is not true, it prints the item just like the choice environment would.

The actual document nests everything within the questions environment, with a series of question items, each of which is followed by a choice list implemented by the choices environment, which wraps around a bunch of items implemented with choice environment.

Work Left to Be Done

The preceding implementation of a quiz is very nice, except that it won't work in LyX, because unlike straight LaTeX, LyX has no provision to nest environments.

To be continued...

Back to Troubleshooters.Com * Back to Linux Library * Litt's LyX Library