Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
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 '12 at 20:50
    
Correct, the numbers will not necessarily be in order. –  mikev Aug 28 '12 at 20:52
    
Concerning not-in-order numbered lists, see tex.stackexchange.com/questions/29850/… –  Torbjørn T. Aug 29 '12 at 7:59
add comment

3 Answers

up vote 3 down vote accepted

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=*}
share|improve this answer
    
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 '12 at 21:07
    
@mikev This could be modified using the leftmargin key of the enumitem package. –  kan Aug 28 '12 at 21:11
    
@mikev see my update –  cmhughes Aug 28 '12 at 21:12
    
@cmhughes I thought mikev wanted the whole environment to flushleft and not just the text, was I wrong? –  kan Aug 28 '12 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 '12 at 21:38
show 2 more comments

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

share|improve this answer
    
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 '12 at 20:55
    
@mikev my example shows how to do it; use the optional argument of \item, as in \item[5.] –  Gonzalo Medina Aug 28 '12 at 20:58
    
yes I see that now, this was very helpful thanks! –  mikev Aug 28 '12 at 21:06
add comment

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

share|improve this answer
    
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 '12 at 21:16
    
@mikev Glad to have helped you. :) –  kan Aug 28 '12 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 '12 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 '12 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 '12 at 21:31
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.