# Writing good looking solutions in math

I write a lot of solutions to math problems and have recently started out with LaTeX.

I have some requirements, I want everything to be left aligned and I want to be able to comment inside the equation environment. So far, I haven't found out the best way to manage this, have a look at my code below:

\documentclass[12pt]{article}
\usepackage[fleqn]{amsmath}
\usepackage{enumitem}
\usepackage{amssymb}
\begin{document}

\subsection*{Exercise 1}
Solve the integrals
\begin{enumerate}[label=\alph*)]
\item $\displaystyle \int\limits_0^1 4x \cdot e^{x^2+3} dx$
\begin{flalign*}
u&=x^2+3, $so that$ \, \frac{du}{dx}=2x &\\
\int & 4x\cdot e^{u}\, \frac{du}{2x} =  2\int e^u \, du = 2e^{x^2+3}+C &\\
\intertext{We now evaluate the limits}
\int\limits_0^1 & 4x\cdot e^{x^2+3} \, dx = 2\left[ e^{x^2+3} \right]\limits_0^1 = 2e^4-2e^3 &
\end{flalign*}
\item $\displaystyle \int \frac{x-6}{x^2-4} \, dx \\$
\begin{flalign*}
\intertext{We use the method of partial fractions}
&\frac{x-6}{x^2-4} = \frac{A}{x+2}+\frac{B}{x-2}&\\
&(x-6)  = A\cdot(x-2) + B\cdot(x+2) &\\
&\intertext{Which gives $A=2$ and $B=-1$, so that}
&\int \frac{x-6}{x^2-4}\, dx = \int \frac{2}{x+2} + \frac{-1}{x-2}\, dx = 2ln|x+2|-ln|x-2|+C &
\end{enumerate}
\end{flalign*}
\end{document}


It will look like this:

There must be a "cleaner" way to do this, right?

• As a retired calculus teacher, who required his students to submit their homework using LaTeX (They submitted both the *.tex and the *.pdf file, along with any files they imputed --- plagiarism prevention), I would have been very happy with your work using LaTeX. Now in an upper division mathematics classroom you would need to do several items to improve the presentation. My recommendation is that you both look at how your textbook presents examples and go to the library and browse through several issues of the Undergraduate Mathematics Journal. – R. Schumacher Apr 15 '15 at 20:29
• Is this question still active? – Dr. Manuel Kuehner Apr 17 '15 at 15:51
• The exam document class is also very good for such things. – Sean Allred Apr 22 '15 at 17:24
• Oh, cool. Just short, what can it do different than my class right now? – Dag Jonas Apr 22 '15 at 19:28

This is my take formatting the MWE. Among other things, it replaces \item with \tag.

\documentclass[12pt]{article}
\usepackage[leqno]{amsmath}
\usepackage{enumitem}
\usepackage{amssymb}

\newcommand{\mytext}[1]% #1 = same as intertext
{&\parbox{0.9\textwidth}{\rule{0pt}{.5\baselineskip}\\
\textrm{#1}\\
\rule{0pt}{.5\baselineskip}}&\\}

\newcounter{exercise}
\newcounter{problem}[exercise]
\newcommand{\myitem}{\stepcounter{problem}\tag*{\alph{problem})}}

\begin{document}

\stepcounter{exercise}
\subsection*{Exercise \theexercise}
Solve the integrals
\begin{flalign*}
\myitem&\int_0^1 4x \cdot e^{x^2+3} dx &\\
&\int 4x\cdot e^{u}\, \frac{du}{2x} =  2\int e^u \, du = 2e^{x^2+3}+C &\\
\mytext{We now evaluate the limits}
&\int_0^1 4x\cdot e^{x^2+3} \, dx = 2\left[ e^{x^2+3} \right]_0^1 = 2e^4-2e^3 &
\end{flalign*}
\begin{flalign*}
\myitem&\int \frac{x-6}{x^2-4}  \, dx &\\
\mytext{We use the method of partial fractions}
&\frac{x-6}{x^2-4} = \frac{A}{x+2}+\frac{B}{x-2} \\
\mytext{But this time the comment just keeps on going and going until it is guaranteed to wrap to the next line.}
&(x-6)  = A\cdot(x-2) + B\cdot(x+2) &\\
\mytext{Which gives $A=2$ and $B=-1$, so that}
&\int \frac{x-6}{x^2-4}\, dx = \int \frac{2}{x+2} + \frac{-1}{x-2}\, dx = 2\ln|x+2|-\ln|x-2|+C &
\end{flalign*}
\end{document}

• I'm not so familiar with the "\stepcounter". My problem now is that if I start a new subsection, the counter resumes. So if exercise 1 was a), b), exercise 2 will continue at c), d),.. – Dag Jonas Apr 22 '15 at 7:09
• There were two problems: First, the equation counter doesn't reset at the end of a subsection. Second, \subsection* neither shows nor increments the subsection counter. – John Kormylo Apr 22 '15 at 17:21
• Great John! Works like a charm now :D – Dag Jonas Apr 22 '15 at 19:45
• When using \mytext, sometimes my comments are too long, and will not break and continue on the next line. I don't know if it's possible to break \intertext ? – Dag Jonas Apr 29 '15 at 8:45
• You can put your comments into a \parbox, but I'm not sure how wide to make it. – John Kormylo Apr 29 '15 at 12:56

## Short Description

Here are three solutions -- see the example.

## Example

\documentclass{article}
\usepackage{parskip}
\begin{document}

\section{Using \texttt{flalign}}

\begin{flalign}
\text{Proof}    && a &= b &&\\
&& a &= b &&\\
&& a &= b && \text{some comment}\\
&& a &= b &&
\end{flalign}

\begin{flalign*}
\text{Proof}    && a &= b &&\\
&& a &= b &&\\
&& a &= b && \text{some comment}\\
&& a &= b &&
\end{flalign*}

\section{Using \texttt{intertext} (amsmath) or \texttt{shortintertext} (mathtools)}

\begin{align}
A &= \int x^2 \text{d}x\\
\intertext{Notice the upright d -- it's an operator and not a variable.}
A &= \int x^2 \text{d}x\\
\shortintertext{Notice the upright d -- it's an operator and not a variable.}
A &= \int x^2 \text{d}x
\end{align}

\noindent\textbf{Taken from the \texttt{amsmath} documentation (Chapter 3.10):}

The command \texttt{intertext} is used for a short interjection of one or two lines
of text in the middle of a multiple-line display structure (see also the \texttt{text}
command in §6). Its salient feature is preservation of the alignment, which
would not happen if you simply ended the display and then started it up again
afterwards. \texttt{intertext} may only appear right after a \textbackslash\textbackslash or \textbackslash\textbackslash* command.

\noindent\textbf{Taken from the \texttt{mathtools} documentation (Chapter 3.5):}

amsmath provides the command \texttt{intertext} for interrupting a multiline display
while still maintaining the alignment points. However the spacing often seems quite
excessive as seen below [\ldots] Using the command \texttt{shortintertext} alleviates this situation somewhat [\ldots]

\end{document}