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!

David Carlisle
  • 757,742
mikev
  • 73
  • 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 http://tex.stackexchange.com/questions/29850/something-like-enumerate-but-with-custom-numbers-at-each-item/29904#29904 – Torbjørn T. Aug 29 '12 at 07:59

3 Answers3

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

Gonzalo Medina
  • 505,128
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=*}
cmhughes
  • 100,947
  • 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
  • Actually, I apologize, after some more test, I can't get the enumerate environment to flushleft with or without leftmargin There is still a small space between the body text and the number from the enumerate environment. – mikev Aug 28 '12 at 21:47
  • Ok, sorry for the amount of comments. I find this to be the most pleasing, \usepackage{enumitem} \setlist[enumerate]{leftmargin=0cm} – mikev Aug 28 '12 at 21:49
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

kan
  • 4,545
  • 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
  • I just realized that the labelsepkey affects the proof environment. Many of the answers are proofs and it creats a large space between "Proof." and the beginning text of the proof. – mikev Aug 28 '12 at 21:39
  • @mikev I'll try and see how to fix that. But on another note, is this how you'd like to see your document: http://imgur.com/CP2KU – kan Aug 28 '12 at 21:53
  • Yes, that I exactly how I'd like to see the document. I tried \usepackage{enumitem} \setlist[enumerate]{leftmargin=0cm} and that works really great, everything looks very nice. – mikev Aug 28 '12 at 22:00
  • @mikev OK, I have failed to fix the way labelsep interferes with proof environment. But, you could incorporate the way I showed in my image in the comment as follows: \begin{enumerate}[labelsep*=2em, leftmargin=0cm]. Hope this helps. May be you could ask another question about how to fix this or I'll ask. – kan Aug 29 '12 at 03:34