# Put a box around an item (homework)

I am using the enumerate environment to do homework. In the following, first I write out the question, then I write out the answers to the first two parts.

What I'd like to do is have a box around the question. How can I do that?

I tried using tikzmark as suggested here but it did not work (the box shows up in the wrong place when I try to use math mode. See the code at the end of the article

\documentclass{article}
\usepackage{amsthm, amssymb, amsfonts, amsmath}

\begin{document}

\begin{enumerate}
\item[1.]
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}
\item[(a)] Find a solution to the problem.
\item[(b)] Regardless of (a), is the solution unique?
\item[(c)] Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item[(d)] Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.

\end{enumerate}

\item[1a.]
$\phi(x) = x$ is a solution, since $$\phi'(x) = 1 = 1 - (x-x)^2.$$

\item[1b.]
Note that the function $1-(y-x)^2$ is $C^{\infty}$ in both $x$ and $y$, and therefore given $x_0$, the function is locally Lipschitz in $y$, uniformly with respect to $x$ for $x$ in a neighborhood of $x_0$. Therefore such a solution is unique.
\end{enumerate}
\end{document}


Using the tikzmark idea (didn't work)

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
\begin{enumerate}
\item[\tikzmark{bl} 1.]
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}
\item[(a)] Find a solution to the problem.
\item[(b)] Regardless of (a), is the solution unique?
\item[(c)] Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item[(d)] Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}
\tikzmark{br}
\item[2.] Something not boxed
\item[3.] Also not boxed.
\end{enumerate}
\tikz[overlay,remember picture]{\draw[red]
($(bl)+(-0.2em,0.9em)$) rectangle
($(br)+(0.2em,-0.3em)$);}
\end{document}


Edit: A problem came when I tried to apply the suggestion given below. tikzmark always wants to put the box on the last page. So if the document is only one page, it is fine. But if it jumps to two pages, it is wrong.

\documentclass{article}
\usepackage{tikz}
\usepackage{enumitem}
\usepackage{amsmath}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
\begin{enumerate}
\item
\leavevmode
\strut
\noindent
\raisebox{\dimexpr\dp\strutbox+\ht\strutbox+1ex}[0pt][0pt]{\tikzmark{bl}}}%%
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}
\item Find a solution to the problem.
\item Regardless of (a), is the solution unique?
\item Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}

\leavevmode
\noindent
\hspace*{\dimexpr\textwidth+1ex}\tikzmark{br}}%%

\item[1a.]
$\phi(x) = x$ is a solution, since $$\phi'(x) = 1 = 1 - (x-x)^2.$$

\item[1b.]
Note that the function $1-(y-x)^2$ is $C^{\infty}$ in both $x$ and $y$, and therefore given $x_0$, the function is locally Lipschitz in $y$, uniformly with respect to $x$ for $x$ in a neighborhood of $x_0$. Therefore such a solution is unique.

\item[1c.]
For convenience of notation, let us agree to label the constant function $0$ as the first Picard iterate $y_1(x)$ rather than the $0$th Picard iterate.
\begin{align*}
y_2(x) &= \int_0^x 1 - (y_1 - \xi)^2 d\xi = x - \frac13 x^3 \\
y_3(x) &= \int_0^x 1 - (y_2(\xi) - \xi)^2 d\xi = \int_0^x 1 - \left(\frac13 \xi^3\right)^2 d\xi = x - \frac{1}{7\cdot 3^2}x^7 \\
y_4(x) &= \dotsb = x - \frac{1}{15\cdot 7^2 \cdot 3^4}x^{15}\\
\vdots \\
y_n(x) &= x - \frac{1}{\prod_{k=1}^{n} (2^k-1)^{2^{(n-k)}}} x^{2^n - 1}
\end{align*}
The iterates will converge pointwise for $|x|<1$ (making no claim that that is the largest such interval). Since the modulus of the second term of $y_n$ is increasing with the modulus of $x$, we can say that the iterates will converge uniformly on compact subsets of the unit interval. (Given $[-a, a]\subseteq (-1,1)$, pick $n$ such that the second term of $y_n(a)$ is less than $\epsilon$; then this $n$ will work for all $x \in [-a, a]$.)

\item[1d.]
Suppose we find an $x$ for which
\begin{align*}
x-\frac{1}{\prod_{k=1}^{n} (2^k-1)^{2^{(n-k)}}} x^{2^n - 1}
\end{align*}
diverges to infinity (of course it will diverge iff the second term diverges). Then by the same considerations as in part (c), we can say that it will diverge for $\xi$ such that $|\xi| > |x|$.

Suppose we choose $x=4$. We have

$y_n(x) = 4 - \frac{2^{2(2^n-1)}}{\prod_{k=1}^{n} (2^k-1)^{2^{(n-k)}}}.$
Note that the denominator satisfies
$(2^n-1)^{2^0}(2^{n-1}-1)^{2^1}\dotsb (2^2-1)^{2^{n-2}}\leq 2^{n2^0 + (n-1)2^1 + \dotsb + (2)2^{n-2}},$
where we have left out the final factor because it is equal to one. Some algebra tells us that $n2^0 + (n-1)2^1 + \dotsb + (2)2^{n-2} = 2^{n}+2^{n-1}-(n+2)\leq 2^{n+1}-(n+2)$. When we consider
$\frac{2^{2(2^n-1)}}{\prod_{k=1}^{n} (2^k-1)^{2^{(n-k)}}} \geq \frac{C2^{2^{n+1}}}{2^{2^{n+1}-(n+2)}}=C2^{n+2},$
this clearly diverges.

\item[2.] Let $y=\phi(x)$ and $y=\psi(x)$ be linearly independent solutions of the ODE $y'' + p(x)y' + q(x)y = 0$, where $p$ and $q$ are continuous on an open interval $I$. Prove the following statements:
\begin{enumerate}
\item[(a)] Suppose that $x_0 \in I$ is a zero of $\phi$, then $\psi$ cannot have a relative extremum value at $x_0$.
\item[(b)] Suppose that $x_0$ and $x_1$ are two consecutive zeros of $\phi$ in $I$. Then $\psi$ must have a zero at some $\xi$, $x_0<\xi<x_1$.

\end{enumerate}

[\textit{Hint:} Think Wronskian.]

\end{enumerate}
\tikz[overlay,remember picture]{\draw[red]
(bl) rectangle
(br);}
\end{document}

-
I wouldn't put the question in the enumerate environment in the first place, especially since you are manually numbering these 'items'. If you want something that relies on TikZ, I recommend tcolorbox. –  jon Apr 4 at 5:08
You'd put it in its own enumerate environment? –  Eric Auld Apr 4 at 5:55
You should apply the overlay as soon as you have finished marking the bounds of the box. Don't wait until later, tikz may remember the dimensions, but it doesn't necessarily remember the page it was defined on. –  A.Ellett Apr 4 at 6:05
A few considerations: you should apply different "names" for each marker, otherwise only the last is remembered (likely the one at the bottom of the page). Second, there are a few versions of \tikzmark: one makes use of customizable offsets. Have a look to Enumerated text and formula with background color for a demo. However, for this I would also use tcolorbox. –  Claudio Fiandrino Apr 4 at 6:10
You should check out the exam class and it's solution environment. –  Sean Allred Apr 4 at 11:24

I would make a number of changes:

1. I would use the enumitem package to facilitate formatting the labels for your enumerate environments.
2. I would use a \label/\ref pair to refer to a previous label

Here's a solution for you box using tikz and a \vadjust

\documentclass{article}
\usepackage{tikz}
\usepackage{enumitem}%%[inline]
\usepackage{amsmath}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
\begin{enumerate}[label=\arabic*.]
\item
\leavevmode
\strut
\noindent
\raisebox{\dimexpr\dp\strutbox+\ht\strutbox+1ex}[0pt][0pt]{\tikzmark{bl}}}%%
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}[label=(\alph*)]
\item Find a solution to the problem.\label{finding a solution}
\item Regardless of \ref{finding a solution}, is the solution unique?
\item Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}

\leavevmode
\noindent
\hspace*{\dimexpr\textwidth+1ex}\tikzmark{br}}%%
\item Something not boxed
\item Also not boxed.
\end{enumerate}
\tikz[overlay,remember picture]{\draw[red]
(bl) rectangle
(br);}
\end{document}


Additionally, I wouldn't use the enumerate environment for numbering homework problems. I would create my own counter and command to do this along the following lines:

\newcounter{myproblemcnt}
\newcommand\problem{\par\refstepcounter{myproblemcnt}%%
\noindent
\textbf{\arabic{myproblemcnt}.}\hspace*{0.25em}}

-
Thanks for your help. I'm coming up against a problem...see the edit. –  Eric Auld Apr 4 at 5:53
Interesting...why wouldn't you use enumerate? (I assume you mean for both the problems and the solutions?) –  Eric Auld Apr 4 at 6:12
@EricAuld in the long run I find it makes things easier to manage or reformat. Also, problems can be numbered independently of other structures like sections and chapters, which may or may not be desirable. I don't believe that would be possible with an enumerate environment. Currently, you may feel you don't need to worry about such things, but in the long run it's best to adopt practices that won't later give you headaches. –  A.Ellett Apr 4 at 6:16
Good tip. Say, would there be a problem in defining a newcommand macro for the entire tikz and vadjust sequence you just defined? Something like \problem? –  Eric Auld Apr 4 at 6:40

As was mentioned in the comments, you can also use the tcolorbox package for creating the box. In my example code, I took over the enumitem package suggestion of A.Ellett.

The code uses your example three times: without box, with box, and finally with a box broken from one page to the following.

Color, line width, arcs etc. of the box can be adapted.

\documentclass{article}

\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage[skins,breakable]{tcolorbox}

oversize,left=8mm,interior hidden,colframe=red,nobeforeafter=,#1}

\begin{document}

\begin{enumerate}[label=\arabic*.]
\item
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}[label=(\alph*)]
\item Find a solution to the problem.\label{finding a solution}
\item Regardless of \ref{finding a solution}, is the solution unique?
\item Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}
\item Something not boxed
\item Also not boxed.
\end{enumerate}

\lipsum[2]

\begin{enumerate}[label=\arabic*.]
\begin{mybox}
\item Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}[label=(\alph*)]
\item Find a solution to the problem.\label{finding a solution}
\item Regardless of \ref{finding a solution}, is the solution unique?
\item Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}
\end{mybox}
\item Something not boxed
\item Also not boxed.
\end{enumerate}

\begin{enumerate}[label=\arabic*.]
\begin{mybox}
\item Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}[label=(\alph*)]
\item Find a solution to the problem.\label{finding a solution}
\item Regardless of \ref{finding a solution}, is the solution unique?
\item Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}
\end{mybox}
\item Something not boxed
\item Also not boxed.
\end{enumerate}

\end{document}


-
I like this idea! Your code is not compiling for me (I tried two editors). I get the following error: Package pgfkeys Error: I do not know the key '/tcb/enhanced jigsaw' and I am going to ignore it. Perhaps you misspelled it. –  Eric Auld Apr 4 at 17:16
@EricAuld sounds like an update is in order: How do I update my TeX distribution? –  cmhughes Apr 4 at 17:41
@EricAuld I'm quite sure that @cmhughes is right and the compilation error is caused by an outdated version. The enhanced jigsaw key was introduced with version 2.60 (2013/12/17). The current version is 2.80 (2014/03/31). Let me know, if you have problems after updating. –  Thomas F. Sturm Apr 5 at 14:58

This is one possible solution, where the second \tikzmark{br} is moved inside the enumerate environment to prevent extra white space. Also the relative distance is adjusted to 5em to cover the whole text.

Code

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
\begin{enumerate}
\item[\tikzmark{bl} 1.]
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}
\item[(a)] Find a solution to the problem.
\item[(b)] Regardless of (a), is the solution unique?
\item[(c)] Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item[(d)] Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$. \tikzmark{br}
\end{enumerate}

\item[2.] Something not boxed
\item[3.] Also not boxed.
\end{enumerate}
\tikz[overlay,remember picture]{\draw[red]
($(bl)+(-0.2em,0.9em)$) rectangle
($(br)+(5em,-0.3em)$);}
\end{document}

-

Based on my answer at How can I add lists and similar in a savebox?, I placed the question in a temporary vbox, and then embraced it in an \fbox, making the appropriate allowances for indentation.

I here define the process as a macro, \boxitem{}:

\def\boxitem#1{\setbox0=\vbox{#1}{\centering\makebox[0pt]{%
\fboxrule=2pt\color{red}\fbox{\hspace{\leftmargini}\color{black}\box0}}\par}}


And then use it inside the enumerate as \boxitem{\item[]...}. Note: \boxitem was REVISED to make it insensitive to succeeding blank lines.

\documentclass{article}
\usepackage{amsthm, amssymb, amsfonts, amsmath, xcolor}% ADDED xcolor
\def\boxitem#1{\setbox0=\vbox{#1}{\centering\makebox[0pt]{%
\fboxrule=2pt\color{red}\fbox{\hspace{\leftmargini}\color{black}\box0}}\par}}
\begin{document}
\begin{enumerate}
\item[1.]
Consider the initial value problem
$\frac{dy}{dx} = 1- (y-x)^2, \qquad y=0 \,\, \text{at} \,\, x=0$
\begin{enumerate}
\item[(a)] Find a solution to the problem.
\item[(b)] Regardless of (a), is the solution unique?
\item[(c)] Write down the Picard iteration scheme for this ODE and find some $r>0$ for which it converges $|x|<r$.
\item[(d)] Find some $R>0$ such that the Picard iterates \textit{diverge} for $|x|>R$.
\end{enumerate}
\item[1a.]
$\phi(x) = x$ is a solution, since $$\phi'(x) = 1 = 1 - (x-x)^2.$$
\item[1b.]
Note that the function $1-(y-x)^2$ is $C^{\infty}$ in both $x$ and $y$, and therefore given $x_0$, the function is locally Lipschitz in $y$, uniformly with respect to $x$ for $x$ in a neighborhood of $x_0$. Therefore such a solution is unique.
\end{enumerate}
\end{document}


POSTSCRIPT: From my exchange with the OP in the comments, I will just add that if one wanted to add a fixed vertical space before and after my \fbox, the definition of \boxitem could be defined as follows, where a space of 3ex is added, by way of example.

\def\boxitem#1{\vspace{3ex}\setbox0=\vbox{#1}{\centering\makebox[0pt]{%
\fboxrule=2pt\color{red}\fbox{\hspace{\leftmargini}\color{black}\box0}}\vspace{3ex}\par}}

-
I think this is the best answer here so far, so thank you. I'm having a little trouble; see "edit 2" to my question. –  Eric Auld Apr 4 at 14:37
@EricAuld Remove the blank line after the closing brace of \boxitem The influence of \hfil and \hfill are dependent on what precedes and follows. I'll see if I can make it less sensitive to blank lines. –  Steven B. Segletes Apr 4 at 14:41
@EricAuld See revised definition of \boxitem, which now uses \centering and \par instead of \hfil and \hfill. –  Steven B. Segletes Apr 4 at 14:50
If I wanted to modify the macro to add some vspace before and after the boxed item, where would I place that command in the macro? –  Eric Auld Apr 4 at 14:54
@EricAuld You can add \vspace{3ex}, for example, right before the \par at the end of the \boxitem definition. –  Steven B. Segletes Apr 4 at 14:57