# Design a multiple columns environment to arrange exercises

I want write the code like:

...
\item
\textbf{Differentiation}.

Differentiate with respect to $x$:
\begin{exercises}{3}
\item$x^{2} - 3x$.
\item$x^{5} - \dfrac{x^3}{3}$.
\item$x + \dfrac{1}{x}$.
...
\end{exercises}

\item
Differentiate with respect to $x$:
\begin{exercises}{2}
\item$y = x\cos\frac{1}{2}x$.
\item$y = \sin\left(\dfrac{\pi x}{180}\right)$.
\item$y = \frac{x^2}{1+x}$.
...
\end{exercises}
...
...


But if I use shortlst package as an advice from An environment which be used to arrange exercises mostly similiar with common enumerate list, the content after second \item Differentiate with respect to $x$ in pic above will typeset in three columns.

----EDIT---- The proposal that use a minipage environment is restricted with the minipage's width defined in advance, that say, it is not suit for different page width. change

\documentclass{article}
\usepackage{shortlst}
\usepackage[width=10cm]{geometry}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\newenvironment{exercises}[1]
{#1:\\ \begin{minipage}[t]{0.9\textwidth}\begin{shortenumerate}}
{\end{shortenumerate}\end{minipage}}
\begin{enumerate}
\item
\begin{exercises}{\textbf{Differentiation}\\ Differentiate with respect to $x$}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercises}
\item
\begin{exercises}{Differentiate with respect to $x$}
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{exercises}
\end{enumerate}
\end{document}


to

\documentclass{article}
\usepackage{shortlst}
\usepackage[width=9cm]{geometry}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\newenvironment{exercises}[1]
{#1:\\ \begin{minipage}[t]{0.9\textwidth}\begin{shortenumerate}}
{\end{shortenumerate}\end{minipage}}
\begin{enumerate}
\item
\begin{exercises}{\textbf{Differentiation}\\ Differentiate with respect to $x$}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercises}
\item
\begin{exercises}{Differentiate with respect to $x$}
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{exercises}
\end{enumerate}
\end{document}


will get

so is the code given by Schweinebacke unless I changed the width individually

\documentclass{article}
\usepackage{shortlst}% http://ctan.org/pkg/shortlst
\usepackage{scrextend}
\usepackage{lipsum}
\begin{document}

\lipsum[1]
\begin{enumerate}
\item {Differentiate with respect to $x$:
\begin{shortenumerate}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{shortenumerate}
}
\item {Differentiate with respect to $x$:
\begin{shortenumerate}
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{shortenumerate}
}
\end{enumerate}
\end{document}


-
Have you considered the multicol package? –  cmhughes Nov 20 '11 at 16:33

The result of your earlier question, possibly in a minipage, and an ordinary enumeration environment should get the result you want.

\documentclass{article}
\usepackage{shortlst}% http://ctan.org/pkg/shortlst
\begin{document}

\newenvironment{exercises}[1]
{#1:\\ \begin{minipage}[t]{0.9\textwidth}\begin{shortenumerate}}
{\end{shortenumerate}\end{minipage}}

\begin{enumerate}
\item
\begin{exercises}{\textbf{Differentiation}\\ Differentiate with respect to $x$}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercises}
\item
\begin{exercises}{Differentiate with respect to $x$}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercises}
\end{enumerate}

\end{document}


which looks like

If you want a table like environment to handle a specific number of columns of equations, then you can define a new counter to do this.

\newcounter{innerlist}
\renewcommand{\theinnerlist}{\arabic{innerlist}}
\newcommand{\inneritem}[1]{\refstepcounter{innerlist}\theinnerlist.\ #1}

\newenvironment{exercises}[2]
{\setcounter{innerlist}{0}\begin{minipage}[t]{0.9\textwidth}#1:\\ \begin{tabular}{#2}}
{\end{tabular}\end{minipage}}

\begin{enumerate}
\item
\begin{exercises}{\textbf{Differentiation}\\ Differentiate with respect to $x$}{ll}
\inneritem{$x^3-3x$} & \inneritem{$x^5-\frac{x^3}{3}$}\\
\inneritem{$x+\frac{1}{x}$} & \inneritem{$(x+1)^2(x-1)^2$}\\
\inneritem{$x\sin x$} & \inneritem{$x\sin x$}
\end{exercises}
\item
\begin{exercises}{\textbf{Differentiation}\\ Differentiate with respect to $x$}{lll}
\inneritem{$x^3-3x$} & \inneritem{$x^5-\frac{x^3}{3}$} & \inneritem{$x+\frac{1}{x}$}\\
\inneritem{$(x+1)^2(x-1)^2$} & \inneritem{$x\sin x$} & \inneritem{$x\sin x$}
\end{exercises}
\end{enumerate}


This version shouldn't have any big issues spreading over pages. Each minipage shouldn't spread between pages, but the items in the outer enumerated list will spread fine.

An alteration using the longtables package allows this to run over pages:

\newenvironment{exercises}[2]
{\setcounter{innerlist}{0}#1:\\ \begin{longtable}[t]{#2}}
{\end{longtable}}

-
Thank you. But a minipage environment can not break page down nicely. –  Tau spit Nov 20 '11 at 11:14
I'm not sure what you mean, can you explain what is wrong with minipage? See the image I included above as an example. –  Mark S. Everitt Nov 20 '11 at 11:23
Note that you must include t as an optional argument to minipage to get the numbers in the right place. –  Mark S. Everitt Nov 20 '11 at 11:24
I mean if the quantity of items inside a minipage is too large, there will occur an overfull. –  Tau spit Nov 20 '11 at 11:29
That doesn't make sense. You can limit the horizontal size of a minipage. In this code I have to 0.9\textwidth. –  Mark S. Everitt Nov 20 '11 at 11:50

I would suggest to combine Mark's answer with package scrextend. That would support page breaks inside the exercise:

\documentclass{article}
\usepackage{shortlst}% http://ctan.org/pkg/shortlst
\usepackage{scrextend}
\usepackage{lipsum}
\begin{document}

\lipsum[1-4]
\begin{enumerate}
\item {Differentiate with respect to $x$:
\begin{shortenumerate}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{shortenumerate}
}
\item {Differentiate with respect to $x$:
\begin{shortenumerate}
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{shortenumerate}
}
\end{enumerate}
\end{document}


Note: If you're already using a KOMA-Script class you don't need to load the package scrextend.

You may define an enviroment for this:

\documentclass{article}
\usepackage{shortlst}% http://ctan.org/pkg/shortlst
\usepackage{scrextend}
\usepackage{lipsum}%

\newenvironment{exercise}[1][\strut]{%
}{%
}
\begin{document}

\lipsum[1]
\begin{enumerate}
\begin{exercise}[{Differentiate with respect to $x$:}]
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercise}
\begin{exercise}[{Differentiate with respect to $x$:}]
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{exercise}
\end{enumerate}
\end{document}


-
If I add \usepackage[b5paper]{geometry} I couldn't get a desired result. –  Tau spit Nov 20 '11 at 12:28
@Tau spit: I get the desired result even if I add \usepackage[b5paper]{geometry} to my MWE. Page break would be earlier, but first has still three columns while second has two columns. Nevertheless you may simply change value 0.1\linewidth and 0.4\linewidth at the example to the value you need to get one, two or three columns as you like. BTW: IMHO b5paper is a strange paper width for a document. a5paper would be more common. In this case you may use .3\linewidth at the second exercise to get two columns. –  Schweinebacke Nov 20 '11 at 12:46
Thanks for your explain. I have edited the question. The problem is I don't want change the width one by one. –  Tau spit Nov 20 '11 at 13:04
@Tau spit: I still do not understand the problem. Why shouldn't the second exercise be set in three columns (that would be, if you simply remove minipage or addmargin from the suggested answers). If you just want little more right indention, just try my changed example with always 2em right indention and three columns for the first exercise but two for the second, without changing the width. –  Schweinebacke Nov 20 '11 at 13:18
I just want my result's appearance is close to the origin document as far as possible. As for the origin doc, why shouldn't the second exercise be set in three columns? I don't know either. Maybe just for neat and tidy. –  Tau spit Nov 20 '11 at 13:32

You could use a combination of multicols with a newlist using the enumitem package, as demonstrated below.

It allows pagebreaks too.

\documentclass{article}
\usepackage{lipsum}
\usepackage{multicol}             % multicols
\usepackage{enumitem}             % customized lists
\usepackage[width=10cm]{geometry} % page dimensions

% setup a new list (terminology from enumitem)
\newlist{mylist}{enumerate}{5}
\setlist[mylist]{label=(\alph*)}

\newenvironment{exercises}[1]{\begin{multicols}{#1}\begin{mylist}}{\end{mylist}\end{multicols}}

\begin{document}

\vspace*{4cm}   % purely to demonstrate the pagebreak
\lipsum[1-2]

\begin{enumerate}
\item {Differentiate with respect to $x$:}
\begin{exercises}{2}
\item $x^3-3x$
\item $x^5-\frac{x^3}{3}$
\item $x+\frac{1}{x}$
\item $(x+1)^2(x-1)^2$
\item $x\sin x$
\end{exercises}
\item Differentiate with respect to $x$:
\begin{exercises}{2}
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\item $y=x\cos \frac{1}{2}x$
\item $y=\sin\left(\frac{\pi x}{180}\right)$
\item $y=\frac{x^2}{1+x}$
\end{exercises}
\end{enumerate}
\end{document}

-

Here is another solution using the {tasks} environment provided by the exsheets package. There is at least one downside to it, though: it cannot safely be nested with other {tasks} environments. (It is not a real list in the LaTeX sense although the syntax is similar.)

The environment has an optional argument that specifies the number of columns:

\begin{tasks}{<num of cols>}
...


Here is the full example:

\documentclass{article}

\SetupExSheets{
% first the number then the title:
% number tasks (a), (b), ...
% make sure the task item labels have enough space:
}
\usepackage{lipsum}% dummy text

\begin{document}

\lipsum[1]\bigskip

\begin{question}[name=Differentiation]
Differentiate with respect to $x$:
\task $x^3-3x$
\task $x^5-\frac{x^3}{3}$
\task $x+\frac{1}{x}$
\task $(x+1)^2(x-1)^2$
\task $x\sin x$
\end{question}

\begin{question}[name=More Differentiation]
Differentiate with respect to $x$:
\task $y=x\cos \frac{1}{2}x$
\task $y=\sin\left(\frac{\pi x}{180}\right)$
\task $y=\frac{x^2}{1+x}$
\task $y=x\cos \frac{1}{2}x$
\task $y=\sin\left(\frac{\pi x}{180}\right)$
\task $y=\frac{x^2}{1+x}$
\task $y=x\cos \frac{1}{2}x$
\task $y=\sin\left(\frac{\pi x}{180}\right)$
\task $y=\frac{x^2}{1+x}$
\task $y=x\cos \frac{1}{2}x$
\task $y=\sin\left(\frac{\pi x}{180}\right)$
\task $y=\frac{x^2}{1+x}$