[Tex/LaTex] Writing good looking solutions in math

math-mode

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:

Result

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

Best Answer

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

item 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 &\\
&u=x^2+3 \quad\textrm{so that}\quad \frac{du}{dx}=2x &\\
&\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}
Related Question