2

The box is exceeds the right boundary probably because of the textwidth. I would like to have it aligned. Besides, what is \noindent for? Why it seems to be the same with or without it.

\begin{enumerate}
\item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
\noindent
\fbox{%
    \parbox{\textwidth}{%
The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
    }%
}
\end{enumerate} 

effect

Tiina
  • 491

3 Answers3

5

Not with parbox directly but with tcolorbox, which respects the current \linewidth automatically. See the various options for the box look.

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{parboxlike}[1][]{%
  enhanced,sharp corners,colback=white,left=3pt,right=3pt,boxsep=0pt,nobeforeafter,before skip=5pt,#1}


\begin{document}
\begin{enumerate}
\item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
\begin{parboxlike}
  The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
\end{parboxlike}

\item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
\begin{parboxlike}[colback=white!80!yellow,title={And now with a title and colourful background},coltitle=black,colbacktitle=cyan]
  The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
\end{parboxlike}

\end{enumerate} 
\end{document}

enter image description here

4

This happens because \textwidth is the whole text width. And when you are in an enumerate environment, some of this width is occupied by the labels.

To get the correct spacing you should use the \textwidth minus the \leftmargin of the enumerate environment.

Also, the \fbox command has an \fboxsep, which is the space between the text and the box. The default for this is 3pt. You should also take this into account when creating the \parbox.

So the final code should be like this:

\documentclass{article}

\begin{document}

\begin{enumerate}
\item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 

\fbox{%
    \parbox{\dimexpr\textwidth-\leftmargin-\itemindent-2\fboxsep}{%
The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
    }%
}
\end{enumerate}

\end{document}

And the result is:

enter image description here

Though, notice that the text outside the box is now wider than the one inside. This is because of the \fboxsep. Now you have another option: set the \fboxsep to 0pt:

\documentclass{article}

\begin{document}

\begin{enumerate}
\item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 

\setlength{\fboxsep}{0pt}
\fbox{%
    \parbox{\dimexpr\textwidth-\leftmargin-\itemindent-2\fboxsep}{%
The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
    }%
}
\end{enumerate}

\end{document}

and the result will be:

enter image description here

It is up to you to decide which one you prefer.

3

Instead of \textwidth, you could use \linewidth and adjust it for the \fbox via -2\fboxsep. This has the advantage that it will work at different list depths:

enter image description here

Code:

\documentclass{article}
\usepackage{showframe}

\begin{document}

\begin{enumerate} \item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country.

\fbox{% \parbox{\dimexpr\linewidth-2\fboxsep\relax}{% The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. }% } \begin{enumerate} \item The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country.

\fbox{%
    \parbox{\dimexpr\linewidth-2\fboxsep\relax}{%
The Young King tells the story of the illegitimate shepherd son of the recently dead king's daughter of an unnamed country. 
    }%
}
\end{enumerate}

\end{enumerate} \end{document}

Peter Grill
  • 223,288