parbox 枚举

parbox 枚举

框超出右边界可能是因为textwidth。我想让它对齐。此外, 是干什么\noindent用的?为什么有它和没有它看起来都一样。

\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} 

影响

答案1

发生这种情况\textwidth是因为所有的文本宽度。当您处于枚举环境中时,部分宽度会被标签占用。

为了获得正确的间距,您应该使用枚举环境的\textwidth减号。\leftmargin

此外,该\fbox命令有一个\fboxsep,即文本和框之间的空间。默认值为3pt。创建 时也应考虑到这一点\parbox

所以最终的代码应该是这样的:

\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}

结果是:

在此处输入图片描述

不过,请注意,框外的文本现在比框内的文本更宽。这是因为\fboxsep。现在您有另一个选择:将 设置\fboxsep0pt

\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}

结果是:

在此处输入图片描述

由您来决定您喜欢哪一个。

答案2

不是parbox直接使用,而是使用tcolorbox,它会自动遵循当前规则\linewidth。查看框外观的各种选项。

\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}

在此处输入图片描述

答案3

除了\textwidth,您还可以使用并针对via\linewidth进行调整。这样做的好处是它可以在不同的列表深度下工作:\fbox-2\fboxsep

在此处输入图片描述

代码:

\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}

相关内容