7

I am typing up homework problems and I would like a very specific format but I'm not sure where to begin. I'd like the exercise numbers to line up in a column and then have the questions and the answers flush left with another "column" so to speak. To get an idea of what I mean this simple code works:

\documentclass{article}

\begin{document}
\begin{tabular}{l l}
    5.  & This is the question          \\
        & This is where the answer goes \\

    11. & Another Questions             \\
        & Another Answer                
\end{tabular}
\end{document}

This looks like what I want, but its annoying to put my whole assignment in a tabular environment and worry about alignment characters, ect. What I would really like is an environment where I can specify the exercise number and it will create the proper layout for the question and answer. Is this possible? Thanks!

3
  • Are the numbers going to be not in order? For instance, you may be assigning the problems 5, 11, 21 and 37 and so on... from a text. Am I right in understanding this?
    – kan
    Aug 28, 2012 at 20:50
  • Correct, the numbers will not necessarily be in order.
    – mikev
    Aug 28, 2012 at 20:52
  • Concerning not-in-order numbered lists, see tex.stackexchange.com/questions/29850/… Aug 29, 2012 at 7:59

3 Answers 3

3

There are lots of ways to do this. A simple way using just an enumerate environment

\begin{enumerate}
  \item[5] This is the question.
  \item[] This is where the answer goes
\end{enumerate}

or perhaps you want your own environment; I've posted a few different options below- take your pick, or perhaps build one based off one of them.

\documentclass{article}
\usepackage{calc}

\newcommand{\question}[1]{\item[#1]}
\newcommand{\answer}{\item[]}
\newenvironment{questionandanswer}[2]{\enumerate\setcounter{enumi}{#2-1}\item#1\item[]}{\endenumerate}
\newenvironment{anotherapproach}{\enumerate}{\endenumerate}


\begin{document}

\begin{questionandanswer}{This is the question.}{5}
  This is where the answer goes
\end{questionandanswer}

\begin{anotherapproach}
  \question{5} This is the question.
  \answer This is where the answer goes
\end{anotherapproach}

\begin{enumerate}
  \item[5] This is the question.
  \item[] This is where the answer goes
\end{enumerate}
\end{document}

If you want to change the indentation of the enumerate environment, then the enumitem package is the most sensible way to go:

\documentclass{article}
\usepackage{calc}
\usepackage{enumitem}
\setlist[enumerate]{leftmargin=*}
7
  • This works great and with so many options! One last question, how to I force the enumerate environment to flushleft? If there is other text on the page the enumerate starts indented further towards the middle. ***Nevermind I got it to work
    – mikev
    Aug 28, 2012 at 21:07
  • @mikev This could be modified using the leftmargin key of the enumitem package.
    – kan
    Aug 28, 2012 at 21:11
  • @mikev see my update
    – cmhughes
    Aug 28, 2012 at 21:12
  • @cmhughes I thought mikev wanted the whole environment to flushleft and not just the text, was I wrong?
    – kan
    Aug 28, 2012 at 21:18
  • @cmhuges, Yes, I tried the leftmargin key and it worked great, but it doesn't work when I use \begin{enumerate}{labelsep=*} to get a nice space between the numbers and the text.
    – mikev
    Aug 28, 2012 at 21:38
4

You can use an enumerate environment; the optional argument of \item allows you to assign the desired label:

\documentclass{article}
\usepackage[nopar]{lipsum}% just to generate text for the example

\begin{document}

\begin{enumerate}
\item This is the question.

And this is where the question goes. \lipsum[2]
\item[11.] This is another question.

And this is where the question goes. \lipsum[4]
\end{enumerate}

\end{document}

enter image description here

3
  • That looks great but how do I change the numbering? My assignments jump around in the exercises, for instance 5, 11, 18, 23
    – mikev
    Aug 28, 2012 at 20:55
  • @mikev my example shows how to do it; use the optional argument of \item, as in \item[5.] Aug 28, 2012 at 20:58
  • yes I see that now, this was very helpful thanks!
    – mikev
    Aug 28, 2012 at 21:06
2

If you're particular about having the spacing between the label, the following code, copied from Gonzalo's answer and modified suitably looks good to me:

\documentclass{article}
\usepackage{enumitem}%provides the key labelsep

\begin{document}

\begin{enumerate}[labelsep=*]
\item[5.] This is the question.

And this is where the question goes. I use \texttt{labelsep}=*
\item[11.] This is another question.

And this is where the question goes. I use \texttt{labelsep}=*
\end{enumerate}

\begin{enumerate}
\item[5.] This is the question.

And this is where the question goes. No \texttt{labelsep}=*
\item[11.] This is another question.

And this is where the question goes. No \texttt{labelsep}=*
\end{enumerate}


\end{document}

An Output:

enter image description here

9
  • Thank you! The label separation looks really nice, I'm using this with a combination of the accepted answer. I really appreciate all the help from all of you!
    – mikev
    Aug 28, 2012 at 21:16
  • @mikev Glad to have helped you. :)
    – kan
    Aug 28, 2012 at 21:21
  • I really like the look that labelsep adds, but when I use it the \setlist{leftmargin=*} (as proposed by cmhughes) no longer works. Do you know how to use labelsep and still have enumerate flush left? Thanks!
    – mikev
    Aug 28, 2012 at 21:24
  • That's exactly what I am experimenting with. @mikev But, I'd like to know if you want the whole enumerate environment (together with the numbers) to shift towards the center...
    – kan
    Aug 28, 2012 at 21:27
  • I'd like the whole environment, including the numbers, to shift away from the center, towards the left side of the page. So that the numbers are flush with the regular text of the page.
    – mikev
    Aug 28, 2012 at 21:31

You must log in to answer this question.

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