2

I have a bunch of exercises written up as seen in the image below. I'd like to write solutions for each exercise directly below where they are written in the .tex document. Then, I want those solutions to be displayed at the end of the document (see the image below). I'd like to achieve exactly the output of the images below. I'm struggling to achieve this. I researched and tried many packages, but nothing seems to fit exactly my formatting.

enter image description here

enter image description here

Here's the code for the first image. The in-line comments indicate how I want to specify each solution. I want the second image (the solutions) to be automatically generated.

\documentclass{article}

%%%%%%% Packages %%%%%%%
\usepackage[margin=0.75in]{geometry}

\usepackage{enumitem}
    % Custom bolded enumerate environment.
    \newenvironment{boenumerate}{\begin{enumerate}[label=\textbf{\arabic*.}]}{\end{enumerate}}
    
    % A clearer method of resuming a numbered/lettered list (\seti and \conti).
    \newcounter{saveenumi}
    \newcommand{\seti}{\setcounter{saveenumi}{\value{enumi}}}
    \newcommand{\conti}{\setcounter{enumi}{\value{saveenumi}}}
    
\usepackage{tasks}  % Make horizontal lists.
    \settasks{label={(\alph*)}, 
        label-align=left, 
        label-offset={0mm}, 
        label-width=25pt, 
        item-indent=25pt, 
        label-format={\bfseries}, 
        after-item-skip=2mm, 
        after-skip={3mm}}

\usepackage{amsmath} % Math mode.


\begin{document}
    \subsection*{Exercises}\addcontentsline{toc}{subsection}{Exercises}
    \subsubsection*{The Tangent Line}
    \begin{boenumerate}
        \item Find the slope of the tangent line at the given point.
        \begin{tasks}(2)
            \task $f(x)=2x^3$,\quad $x=\tfrac{1}{3}$
                %\solution{$f^\prime(x)=6x^2 \implies f^\prime(\tfrac{1}{3})=\tfrac{2}{3}$}
            \task $y=(1-2x)(3x-4)$,\quad $x=2$
                %\solution{$-13$}
        \end{tasks}
        
        \item Find the equation of the tangent line at the given point.
        \begin{tasks}(2)
            \task $y=2\sqrt{x}$, $(9,6)$
                %\solution{$y=\tfrac{1}{3}(x-9)+6$}
            \task $xy=1$, $\left(5,\tfrac{1}{5}\right)$
                %\solution{$y=\tfrac{-1}{25}(x-5)+\tfrac{1}{5}$}
        \end{tasks}
        \item At what point on the parabola $y=4x^2$ is the slope of the tangent line equal to 4?
            %\solution{$(\tfrac{1}{2},1)$}
        \item Find the point on the curve $y=x\sqrt{x}$ where the tangent line is parallel to the line $6x-y=4$.
            %\solution{$(16,64)$}
        \seti
    \end{boenumerate}
    
    \subsubsection*{The First Derivative Test}
    \begin{boenumerate}
        \conti
        \item Find the intervals of increase and decrease for the following functions.
        \begin{tasks}(3)
            \task $f(x)=3x^2-18x+1$
                %\solution{Increasing: $(3,\infty)$; Decreasing: $(-\infty,3)$}
            \task $f(x)=2x^3-9x^2-60x+82$
                %\solution{Increasing: $(-\infty,-2)\cup (5,\infty)$; Decreasing: $(-2,5)$}
        \end{tasks}
    \end{boenumerate}
\end{document}

1 Answer 1

2

I've come up with a nice solution to my problem. It's fairly comprehensive, so I figured I'd answer my own question for others who might find this useful.

\documentclass{article}

%%% Packages %%%
\usepackage[margin=0.75in]{geometry} % Set the page margins
\usepackage{lipsum} % pseudo text
\usepackage{scrextend} % To use the \addmargin command
\usepackage{newfile} % Write and read from a tex file from within this file
\usepackage{amsmath} % math mode
\usepackage{xifthen} % conditionals
\usepackage{enumitem} % A better enumerate environment.
    \newenvironment{boenumerate}    % Custom bolded enumerate environment.
    {\begin{enumerate}\renewcommand\labelenumi{\textbf\theenumi.}}
        {\end{enumerate}}
    \newcounter{saveenumi}
    \newcommand{\seti}{\setcounter{saveenumi}{\value{enumi}}}
    \newcommand{\conti}{\setcounter{enumi}{\value{saveenumi}}}
\usepackage{setspace} % Make the solutions 1.5 spaced
\usepackage{tasks}  % Make horizontal lists.
    \settasks{label={(\alph*)}, 
        label-align=left, 
        label-offset={0mm}, 
        label-width=25pt, 
        item-indent=25pt, 
        label-format={\bfseries}, 
        after-item-skip=2mm, 
        after-skip={3mm}}
    
%%% Commands and Settings %%%
% Solutions file
\newoutputstream{solutions}
\openoutputfile{\jobname.solutions}{solutions}

% Setting the subheader: \exheader{name of subheader}
\newcommand{\exheader}[1]{%
    \subsubsection*{#1}%
    \addtostream{solutions}{\textbf{#1}\newline}%
}

% Making a solution command that reports the solution at the end of the document
\newcounter{itemCounter}
\newcommand{\solution}[1]{%
    \ifthenelse{\value{task} = 1}{%
        \addtostream{solutions}{
        \textbf{\theenumi.} \textbf{\thetask} \vspace{-1.75em} \begin{addmargin}[3.5em]{0em} \unexpanded{#1}\end{addmargin}}%
        \setcounter{itemCounter}{\value{enumi}}%
        }{%
        \ifthenelse{\value{task}>1 \and \value{enumi} = \value{itemCounter}}{%
            \addtostream{solutions}{\phantom{\textbf{\theenumi.}} \textbf{\thetask} \vspace{-1.75em} \begin{addmargin}[3.5em]{0em} \unexpanded{#1}\end{addmargin}}%
            \setcounter{itemCounter}{\value{enumi}}%
            }{%
                \addtostream{solutions}{\textbf{\theenumi.} \vspace{-1.75em} \begin{addmargin}[1.5em]{0em} \unexpanded{#1}\end{addmargin}}%
            }%
        }%
    }

% Removing indenting for the whole document
\setlength{\parindent}{0pt}

\begin{document}
    \subsection*{Exercises}\addcontentsline{toc}{subsection}{Exercises}
    \exheader{The Tangent Line}
    \begin{boenumerate}
        \item Find the slope of the tangent line at the given point.
        \begin{tasks}(2)
            \task $f(x)=2x^3$,\quad $x=\tfrac{1}{3}$
                \solution{$f^\prime(x)=6x^2 \implies f^\prime(\tfrac{1}{3})=\tfrac{2}{3}$}
            \task $y=(1-2x)(3x-4)$,\quad $x=2$
                \solution{$-13$}
        \end{tasks}
        
        \item Find the equation of the tangent line at the given point.
        \begin{tasks}(2)
            \task $y=2\sqrt{x}$, $(9,6)$
                \solution{$y=\tfrac{1}{3}(x-9)+6$}
            \task $xy=1$, $\left(5,\tfrac{1}{5}\right)$
                \solution{$y=\tfrac{-1}{25}(x-5)+\tfrac{1}{5}$}
        \end{tasks}
        \item At what point on the parabola $y=4x^2$ is the slope of the tangent line equal to 4?
            \solution{$(\tfrac{1}{2},1)$}
        \item Find the point on the curve $y=x\sqrt{x}$ where the tangent line is parallel to the line $6x-y=4$.
            \solution{$(16,64)$}
        \seti
    \end{boenumerate}
    
    \exheader{The First Derivative Test}
    \begin{boenumerate}
        \conti
        \item Find the intervals of increase and decrease for the following functions.
        \begin{tasks}(3)
            \task $f(x)=3x^2-18x+1$
                \solution{Increasing: $(3,\infty)$; Decreasing: $(-\infty,3)$}
            \task $f(x)=2x^3-9x^2-60x+82$
                \solution{Increasing: $(-\infty,-2)\cup (5,\infty)$; Decreasing: $(-2,5)$}
        \end{tasks}

        \item Question prompt \solution{\lipsum[1]}
        \item Question prompt:
        \begin{tasks}
            \task Some question \solution{\lipsum[2]}
            \task Another question \solution{\lipsum[3]}
        \end{tasks}
    \end{boenumerate}

    \clearpage

    \section*{SOLUTIONS}
    \setstretch{1.5}
    \closeoutputstream{solutions}
    \input{\jobname.solutions}
\end{document}

The code above produces the following two pages: enter image description here enter image description here

Here's a brief explanation of the method:

  1. We use the newfile package to make use of the \addtostream command. The idea is to write the current state of \theenumi (the current item number of enumerate) and \thetask (the current task number of tasks) to a different output stream and then display this output stream at the end.
  2. The \solution command is doing all of the heavy lifting here. Based on the current task value, it writes the solutions with or without the current item value.
  3. We make use of the scrextend package to use \addmargin to indent the solutions as an itemized list.
  4. The \unexpanded command is used to ensure that special characters are preserved correctly. This way one can include TikZ images and figure environments.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .