
我使用 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}
產生的輸出如下所示:
有沒有比重新排列句子直到不再產生問題更好的解決方案來避免這種 Overfull \hboxes ?
答案1
Asloppypar
會暫時讓你克服這個問題。對於任何想要在行尾排版的超大框框都會出現這個問題。
該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}