如何防止 \fbox 超出页面范围?

如何防止 \fbox 超出页面范围?

我正在使用该\fbox命令来框住一大块文本:

\fbox{Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.}

此文本不会换行。它只是超出页面范围。为什么会这样?

答案1

mdframed我认为使用处理换行和分页的包可能会更好。

请参阅下面的 MWE 和文档以获取更详细的示例

\documentclass{article}

\usepackage{mdframed}

\begin{document}

\begin{mdframed}
Four score and seven years ago our fathers brought forth 
on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
\end{mdframed}

\end{document}

答案2

您可以使用\parbox以下方式作为内容:

  1. 如果您不关心我通常使用的精确尺寸:

    \fbox{\parbox{0.90\linewidth}{...}}

  2. 如果您希望文本仍然占据整个行宽并进入\fbox边距,那么您可以使用@Werner 的建议:

    \noindent\makebox[\linewidth][l]{\hspace{\dimexpr-\fboxsep-\fboxrule\relax}\fbox{\parbox{\linewidth}{...}}}

  3. 如果您希望\fbox保持在边距边界内,可以使用:

    \noindent\fbox{\parbox{\dimexpr\linewidth-2\fboxsep-\fboxrule\relax}{...}}

[showframe]以下是我使用以下选项的结果包裹geometry显示边距:

在此处输入图片描述

笔记:

代码:

\documentclass{article}
\usepackage{xcolor}
\usepackage[showframe]{geometry}

\newcommand{\FboxText}{Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.}

\begin{document}
\fbox{\parbox{0.90\linewidth}{\FboxText}}

\bigskip
\noindent\makebox[\linewidth][l]{\hspace{\dimexpr-\fboxsep-\fboxrule\relax}\fbox{\parbox{\linewidth}{\FboxText}}}

\bigskip
\noindent\fbox{\parbox{\dimexpr\linewidth-2\fboxsep-\fboxrule\relax}{\FboxText}}
\end{document}

答案3

如果您不希望框架文本跨页面,您可以使用adjustbox以下包:

\documentclass{article}
\usepackage{adjustbox}
\begin{document}
\adjustbox{minipage=\linewidth-2\fboxsep-2\fboxrule,fbox}{Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.}
\end{document}

这会将内容放置在minipage当前行宽减去框架所需空间的环境中。如果您希望文本与普通文本一样宽,并且框架应该进入边距,请使用\adjustbox{minipage=\linewidth,fbox,center}{..}

当然,如果您不想加载其他包,也可以手动使用合适的包,但这样minipage可以让 live 更加轻松。查看手册以了解更多有用的键。\fboxadjustbox

相关内容