我怎样才能做这样的事情(\fbox
可以跨越多行):
|---------------------------------------------
The quick brown fox |jumps over the| lazy dog. The quick brown fox
|---------------------------------------------
--------------|
jumps over the| lazy dog. The quick brown fox jumps over the lazy dog.
--------------|
The quick brown fox jumps over the lazy dog.
梅威瑟:
\documentclass{article}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \fbox{Sed a leo ut erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla hendrerit.} Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim. Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat vitae velit pharetra lobortis. Donec ut erat a erat porta aliquet. Nulla eget augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam. Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti. In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium, lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel dolor.
\end{document}
答案1
这绝不是非常酷的帖子Bobyandbob 指出。相反,我试图说服自己,人们可以用 Ti 来实现这一点钾Z“仅”。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikzpagenodes}
\makeatletter
\newcommand{\gettikzxy}[3]{% from https://tex.stackexchange.com/a/58590/121799
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\global\edef#2{\the\pgf@x}%
\global\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\BoxMe}[2][2pt]{%
\tikz[remember picture,overlay, baseline=(Begin.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}#2%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
\begin{tikzpicture}[overlay,remember picture]
\gettikzxy{($(Begin.north)-(current page.south west)$)}{\BeginxN}{\BeginyN}
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}%\typeout{\BeginyN\space\EndyN}
\pgfmathtruncatemacro{\mytest}{\EndyN-\BeginyN}
\ifnum\mytest=0\relax% begin and and in the same line %\typeout{begin and end in the same line}
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- cycle;
\else% \typeout{end below begin}
\path (current page text area.north west) -- (current page text area.south west)
node(WestLine)[left]{};
\path (current page text area.north east) -- (current page text area.south east)
node(EastLine)[right]{};
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}
\gettikzxy{($(Begin.south)-(current page.south west)$)}{\BeginxS}{\BeginyS}
\pgfmathtruncatemacro{\mytest}{\BeginyS-\EndyN+1pt}% \typeout{\mytest}
\ifnum\mytest<2\relax% \typeout{end in the next line after begin}%
\pgfmathtruncatemacro{\mytest}{\BeginxS-\EndxN}% \typeout{\mytest}
\ifnum\mytest>0\relax%
\draw[thick,-] (Begin.north -| EastLine) -- ($(Begin.north)-(#1,0)$) --
($(Begin.south)-(#1,0)$) -- (Begin.south -| EastLine);
\draw[thick,-] (End.south -| WestLine) -- ($(End.south)+(#1,0)$) --
($(End.north)+(#1,0)$) -- (End.north -| WestLine);
\else
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) --
(Begin.south -| WestLine) -- (End.south -| WestLine)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
-- (Begin.north -| EastLine) -- cycle;
\fi
\else
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) --
(Begin.south -| WestLine) -- (End.south -| WestLine)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
-- (Begin.north -| EastLine) -- cycle;
\fi
\fi
\end{tikzpicture}}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \BoxMe{Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit.} Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. \BoxMe{Suspendisse placerat
vitae velit pharetra lobortis et marmottae.} Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
\BoxMe{Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at.} Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.
\end{document}
只要框从一行开始,在下一行结束,并且没有水平重叠,它就会按照您的建议执行框,请参见上图中的第二个框。如果有水平重叠,或者框超过两行,它会以我个人喜欢的方式关闭。非常欢迎提出批评或改进建议,我很乐意修改此代码片段!(此时,我注意到两个缺点。首先,如果框应该越过分页符,这显然不起作用。其次,如果框超过一行,并且从一行的最左侧开始和/或结束在一行的最右侧,结果就不太好看。这两个问题都可以修复,但我要等到收到更多评论后再修复。)