如下面的代码所示,当 tcbox 水平溢出页面时,它不会自动移动到新行:
This is some text to fill the empty space before the box. Some more text 1234.
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
我可以使用以下方法手动将其移动到新行\\
:
This is some text to fill the empty space before the box. Some more text 1234.\\
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
然而,这很麻烦,因为每当我对文本进行更改时,我都必须不断检查我的 tcbox 是否溢出。
如果整个 tcbox 溢出页面,是否有办法让它自动呈现到新行上,而无需我手动执行\\
?
谢谢。
为史蒂文编辑:
虽然该宏确实有效,但 tabto 命令似乎存在一些严重的问题,会弄乱 tcbox 垂直间距。
~\\
This is some text to fill the empty space before the box. Some more text 1234.
\tcbox[on line]{The tcbox overflows the page 123456789.} This is more text.
aaaaaaaaaaaaaaaaaaaaaaaaaaaa space aaaaaa
\tcbox[on line]{tcbox on the line below.}
Text after the box.
~\\
This is some text to fill the empty space before the box. Some more text 1234.
\tccheckbox{The tcbox overflows the page 123456789.} This is more text.
aaaaaaaaaaaaaaaaaaaaaaaaaaaa space aaaaaa \tccheckbox{tcbox on the line below.}
Text after the box.
正如您所看到的,在使用 tabto 命令之后在下面的行上使用 tcbox 会导致它剪切到上面的行中,而如果不使用 tabto,垂直间距是正确的。
答案1
这里我在排版之前保存了框并测量了它的宽度。我使用tabto
包来确定到行尾的距离并与框宽度进行比较。
我也使用过,\sloppy
因为 tcbox 大小对于内联排版来说非常笨拙。
已编辑以解决重叠问题。必须明白,\tabto
调用本质上会破坏段落的排版,否则会解决 tcbox 的垂直空间问题。因此,我为解决这个问题所做的是立即排版 tcbox 垂直占用空间的零宽度规则前并立即后执行该\tabto
序列。
好消息是,这解决了垂直重叠问题。(潜在的)坏消息是,在没有\tabto
调用的情况下,它不再必然排版为与序列完全相同。同样,原因是每当\tabto
调用 a 时,未调整的位置都是固定的,并且不能再作为段落边距的一部分水平移动。因此,该点之前的文本被排版为以该点结束的段落,而 tcbox 之后的文本开始新的文本对齐序列。
\documentclass{article}
\usepackage[margin=1.5in,showframe]{geometry}
\usepackage{tcolorbox,tabto}
\newcommand\tccheckbox[2][on line]{%
\sbox0{\tcbox[#1]{#2}}%
\rule[-\dp0]{0pt}{\dimexpr\dp0+\ht0}%
\tabto*{\linewidth}%
\ifdim\dimexpr\linewidth-\TabPrevPos > \wd0\relax
\tabto*{\TabPrevPos}%
\else
\tabto{0pt}%
\fi
\copy0%
\rule[-\dp0]{0pt}{\dimexpr\dp0+\ht0}%
\allowbreak
}
\begin{document}
\sloppy
Some text before the box.
\tccheckbox{The tcbox overflows the page 123456789.}
Text after the box.
\bigskip This is some text to fill the empty space before the box.
\tccheckbox{The tcbox overflows the page 123456789.}
Text after the box.
~\\
This is some text to fill the empty space before the box. Some more text 1234.
\tcbox[on line]{The tcbox overflows the page 123456789.} This is more text.
aaaaaaaaaaaaaaaaaaaaaaaaaaaa space aaaaaa
\tcbox[on line]{tcbox on the line below.}
Text after the box.
~\\
This is some text to fill the empty space before the box. Some more text 1234.
\tccheckbox{The tcbox overflows the page 123456789.} This is more text.
aaaaaaaaaaaaaaaaaaaaaaaaaaaa space aaaaaa \tccheckbox{tcbox on the line below.}
Text after the box.
\end{document}
答案2
一种可能的解决方案是\raggedright
使用ragged2e.sty
:
\documentclass{article}
\usepackage[margin=1.5in,showframe]{geometry}
\usepackage{tcolorbox}
\usepackage[document]{ragged2e}
\usepackage{lipsum}
\begin{document}
This is some text to fill the empty space before the box.
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
\lipsum[1][1-5]
\bigskip This is some text to fill the empty space before the box.
\justifying
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
\lipsum[1][1-5]
\bigskip This is some text to fill the empty space before the box.
\raggedright
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
\lipsum[1][1-5]
\bigskip This is some text to fill the empty space before the box.
\raggedleft
\tcbox[on line]{The tcbox overflows the page 123456789.}
Text after the box.
\lipsum[1][1-5]
\end{document}