我使用 TikZ 解决方案的修改版本另一个问题在一些文本周围绘制圆角矩形,将其标记为程序的按钮。对于单个字符来说没有问题,但如果我使用较长的术语(因为按钮在我的程序中被命名),我会遇到 Overfull \hboxes 的问题。
梅威瑟:
\documentclass[a4paper,10pt]{scrartcl}
\usepackage{tikz}
\newcommand*\rectangled[1]{\tikz[baseline=(char.base)]{
\node[shape=rectangle,draw,inner sep=2pt, rounded corners=4pt, thick] (char) {\sffamily{#1}};}}
\begin{document}
A rectangled number \rectangled{1}: No problem normally as it is used as normal charakter and TeX can set it in the right position.
A button with a longer name is marked using the same command and \rectangled{creates an Overfull \textbackslash hbox} if it is set to the end of a line.
\end{document}
产生的输出如下所示:
有没有更好的解决方案来避免这种过满的 \hboxes,而不是重新排列句子直到不再产生问题?
答案1
可以sloppypar
暂时帮你解决这个问题。任何超大尺寸的框如果要在行尾排版,就会出现这个问题。
此sloppypar
功能(或\sloppy
整个文档)改变了 TeX 的惩罚,更加注重避免边距超限,但代价是单词间空间过宽。没有“免费午餐”。
\documentclass[a4paper,10pt]{scrartcl}
\usepackage{tikz}
\newcommand*\rectangled[1]{\tikz[baseline=(char.base)]{
\node[shape=rectangle,draw,inner sep=2pt, rounded corners=4pt, thick] (char) {\sffamily#1};}}
\begin{document}
A rectangled number \rectangled{1}: No problem normally as it is used as normal charakter and TeX can set it in the right position.
\begin{sloppypar}
A button with a longer name is marked using the same command and \rectangled{creates an Overfull \textbackslash hbox} if it is set to the end of a line.
\end{sloppypar}
\end{document}
答案2
这是一个允许换行并在两个部分都使用开放矩形的版本。\tikzmark
用于标记文本的开始和结束,并且在文本排版后绘制椭圆:
笔记:
这确实需要两次运行。第一次确定位置,第二次进行绘图。
如果文本跨越页面边界,则此方法无效。
的值
\RoundedCorner
不能大于当前的值2.0pt
,否则圆角会出现瑕疵。arc
如果需要更大的半径,也许可以使用手动绘制的。\InnerSep
可以调整的值来调整添加的额外水平间距前和后文本。来自
\tikzmark
在正文旁边添加大括号。您可以取消注释包裹
showframe
查看页边距。
代码:
\documentclass{article}
%\usepackage{showframe}% to see page boundaries
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{decorations.pathreplacing}
\newcommand*{\InnerSep}{1pt}% Only applied to x directions
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[inner sep=0] (#1) {};}
%% https://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
\newdimen\XCoordA
\newdimen\YCoordA
\newdimen\XCoordB
\newdimen\YCoordB
\newcommand*{\ExtractCoordinate}[3]{\path (#3); \pgfgetlastxy{#1}{#2};}%
\newcommand*{\RoundedCorner}{2.0pt}% <-- MUST NOT BE ANY LARGER
\tikzset{My Line Style/.style={rounded corners=\RoundedCorner, thick, blue}}
\newcommand*\rectangled[2][]{%
\tikzmark{Start Mark}#2\tikzmark{End Mark}%
\begin{tikzpicture}[overlay,remember picture]
\ExtractCoordinate{\XCoordA}{\YCoordA}{Start Mark}
\ExtractCoordinate{\XCoordB}{\YCoordB}{End Mark}
\ifdim\YCoordA=\YCoordB% Starts and ends on same line
\draw[My Line Style,#1]
([shift={(-\InnerSep,-0.3*\baselineskip)}]Start Mark.south east)
rectangle
([shift={(\InnerSep,0.7*\baselineskip)}]End Mark.north west)
;
\else% Starts on a different line
\coordinate (Right Hand Edge) at (Start Mark -| current page text area.east);
\coordinate (Left Hand Edge) at (End Mark -| current page text area.west);
\draw [My Line Style,#1]% Draw start of oval rectangle
([yshift=-0.3*\baselineskip]Right Hand Edge)
-| ([xshift=-\InnerSep]Start Mark.west)
|- ([yshift=0.7*\baselineskip]Right Hand Edge)
;
\draw [My Line Style,#1]% Draw end of oval rectangle
([yshift=-0.3*\baselineskip]Left Hand Edge)
-| ([xshift=\InnerSep]End Mark.east)
|- ([yshift=0.7*\baselineskip]Left Hand Edge)
;
\fi%
\end{tikzpicture}%
}
\begin{document}
A rectangled number \rectangled{1}, or \rectangled[magenta]{word}: No problem
normally as it is used as normal character and TeX can set it in the right position.
A button with a longer name marked using the same command \rectangled[red]{used to
create an Overfull \textbackslash hbox} if it was set to the end of a line.
A button with a even longer name \rectangled[brown]{no longer creates a Overfull
\textbackslash hbox when it crosses a line} boundary.
\end{document}