テキスト内の文字としての TikZ ノード

テキスト内の文字としての TikZ ノード

私はTikZソリューションの修正版を使用しています別の質問テキストの周囲に角丸の四角形を描画して、プログラムのボタンとしてマークします。1 文字の場合は問題ありませんが、長い用語を使用すると (プログラム内でボタンに名前が付けられている場合)、Overfull \hboxes の問題が発生します。

MWE:

\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}

生成された出力は次のようになります。 MWEの出力

問題が発生しなくなるまで文を並べ替えるよりも、このような Overfull \hboxes を回避するためのより良い解決策はありますか?

答え1

A を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テキストの開始と終了をマークするために使用され、楕円はテキストがタイプセットされた後に描画されます。

ここに画像の説明を入力してください

ノート:

  • これには 2 回の実行が必要です。1 回目は場所を決定するため、2 回目は描画を行うためです。

  • テキストがページの境界を越える場合、これは機能しません。

  • の値は、\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}

関連情報