调整 TikZ txt 样式中逗号和句号的位置

调整 TikZ txt 样式中逗号和句号的位置

在下面的示例中,可以看到 TikZ 环境中基线上单词后面的逗号和句号取决于单词上方的节点向右延伸的距离。我该如何更改此设置,以便逗号和句号不关心单词上方的节点,而只关心单词本身?

\documentclass{article}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\newcommand{\phraselabel}[2]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight, draw},
        above/.style = {inner sep = 0pt, text depth = 0pt, draw}%
        ]
    \node[txt] (word) {#1};
    \node[above] at (word.north) {\footnotesize{#2}};
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{bb}{xxxxx}, \phraselabel{bb}{xxxxx}.
\end{document}

在此处输入图片描述

答案1

下面的例子让我们\phraselabel向前看,看看后面是否有逗号或句号。如果找到标点符号,则将其作为参数读取并放在单词后面。

示例的第一行使用了带框的版本,但标点符号留在了外面,因为它不属于该单词。在这种情况下,标点符号应该在框内,或者框只是为了调试,第二行显示了没有框的简化版本。

实现了另一个功能,即处理空格因子。为了在句号后添加更大的空格\nonfrenchspacing,TeX 会跟踪空格因子。该示例保存了节点内单词后的空格因子,并在 后恢复它tikzpicture。请参见“cc.”和“New”之间更大的空格。

完整示例:

\documentclass{article}
\usepackage{ltxcmds}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\makeatletter
\newcommand{\phraselabel}[2]{%
    \ltx@ifnextchar@nospace,{\@phraselabel{#1}{#2}}{%
    \ltx@ifnextchar@nospace.{\@phraselabel{#1}{#2}}{%
    \ltx@ifnextchar@nospace;{\@phraselabel{#1}{#2}}{%
    \ltx@ifnextchar@nospace!{\@phraselabel{#1}{#2}}{%
    \ltx@ifnextchar@nospace?{\@phraselabel{#1}{#2}}{%
        \@phraselabel{#1}{#2}{}%
    }}}}}%
}
\newcommand*{\@phraselabel}[3]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight, draw},
        above/.style = {inner sep = 0pt, text depth = 0pt, draw}%
        ]
    \node[txt] (word) {#1\phrase@save@spacefactor};
    \ifx\\#3\\
    \else
      \node[anchor=base, right, inner sep=0pt]
        at (word.base east)
        {\phrase@set@spacefactor#3\phrase@save@spacefactor};
    \fi
    \node[above] at (word.north) {\footnotesize{#2}};
    \end{tikzpicture}%
    \phrase@set@spacefactor
}
\newcount\phrase@spacefactor
\newcommand*{\phrase@save@spacefactor}{%
    \global\phrase@spacefactor=\spacefactor
}
\newcommand*{\phrase@set@spacefactor}{%
    \spacefactor=\phrase@spacefactor
}
\makeatother

\begin{document}

% With boxes
\phraselabel{aa}{xxxxx}
\phraselabel{bb}{yyyyy},
\phraselabel{cc}{zzzzz}.
\phraselabel{New}{xxxxx}
\phraselabel{sentence}{yyyyy}.

% Without boxes
\makeatletter
\renewcommand*{\@phraselabel}[3]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight},
        above/.style = {inner sep = 0pt, text depth = 0pt}%
        ]
    \node[txt] (word) {#1#3\phrase@save@spacefactor};
    \node[above] at (word.north) {\footnotesize{#2}};
    \end{tikzpicture}%
    \phrase@set@spacefactor
}
\makeatother
\phraselabel{aa}{xxxxx}
\phraselabel{bb}{yyyyy},
\phraselabel{cc}{zzzzz}.
\phraselabel{New}{xxxxx}
\phraselabel{sentence}{yyyyy}.
\end{document}

结果

评论:

  • \ltx@ifnextchar@nospaceltxcmds用于向前查看。与 LaTeX 的相反\@ifnextchar,它在向前查看时不会吞噬空格。

答案2

您可以使用\rlap{}

\documentclass{article}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\newcommand{\phraselabel}[2]{%
    \begin{tikzpicture}[%
        baseline = (word.base),%
        txt/.style = {inner sep = 0pt, text height = \Aheight},%
        tag/.style = {above=0.75ex, inner sep = 0pt, text depth = 0pt}%
        ]%
    \node[txt] (word) {#1};%
    \node[tag] at (word.north) {\footnotesize{#2}};%
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{gg}{xxxxx} \phraselabel{aa}{jjjjj} \phraselabel{tt}{xxxxx} \phraselabel{bb\rlap{.}}{xxxxx}
\end{document}

在此处输入图片描述

答案3

作为一个快速的技巧,你可以使用 \hphantom{}

\documentclass{article}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\newcommand{\phraselabel}[2]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight, draw},
        above/.style = {inner sep = 0pt, text depth = 0pt, draw}%
        ]
    \node[txt] (word) {#1};
    \node[above] at (word.north) {\footnotesize{#2}};
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{\hphantom{,}bb,}{xxxxx} \phraselabel{\hphantom{.}bb.}{xxxxx}
\end{document}

在此处输入图片描述

相关内容