对于我图纸中的所有标签,我使用通用设置
every label/.style = {label distance=1mm, inner sep=0mm,
font=\footnotesize\sffamily, align=center,
text=teal!60!black},
如果标签在一行或多行中,这也能正常工作,直到我不使用 TikZ 库babel
。 如果标签节点中有一行标签,则会引入空白:
这是错误还是我在标签样式定义中遗漏了什么?如何消除这个空格?解决方案是align=center
在需要时不使用 babel 或本地使用,由于某种原因不得不将其排除在考虑之外。
上图的 MWE 为:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{babel}
\begin{document}
\begin{tikzpicture}[
every label/.style = {label distance=1mm, inner sep=0mm,
font=\footnotesize\sffamily, align=center,
text=teal!60!black},
dot/.style = {circle, fill=black, inner sep=0mm, outer sep=0mm,
minimum size=1mm,
node contents={}},
]
% axes
\draw[->] (-1.7,0) -- (2.4,0) node[fill=cyan,below left] {$\phi_1(t)$};
\draw[->] (0,-1.7) -- (0,2.4) node[fill=cyan,below left] {$\phi_2(t)$};
\draw[densely dashed,very thin] (0,0) circle (13mm);
% signals
\foreach \i [count=\ix from 1] in {0,90,180,270}
\draw (\i:13mm) node[dot,label={[fill=yellow]\i+45:$s_{\ix}$}];
%----------------
\end{tikzpicture}
\end{document}
答案1
该库激活该选项handle active characters in nodes
,它用于\scantokens
处理具有不同 catcode 的节点的内容。\scantokens
(如\input
)在末尾插入一个空格。您可以在末尾使用一些命令来抑制它,这些命令将占用以下空格,例如\ignorespaces
或一个简单的\relax
:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{babel}
\begin{document}
\begin{tikzpicture}[
every label/.style = {label distance=1mm, inner sep=0mm,
font=\footnotesize\sffamily, align=center,
text=teal!60!black},
dot/.style = {circle, fill=black, inner sep=0mm, outer sep=0mm,
minimum size=1mm,
node contents={}},
]
% axes
\draw[->] (-1.7,0) -- (2.4,0) node[fill=cyan,below left] {$\phi_1(t)$};
\draw[->] (0,-1.7) -- (0,2.4) node[fill=cyan,below left] {$\phi_2(t)$};
\draw[densely dashed,very thin] (0,0) circle (13mm);
% signals
\foreach \i [count=\ix from 1] in {0,90,180,270}
\draw (\i:13mm) node[dot,label={[fill=yellow]\i+45:$s_{\ix}$\relax}];
%----------------
\end{tikzpicture}
\end{document}
编辑
实际上我认为这是一个错误。使用 \scantokens 时,pgf 应该隐藏文件结束标记。这可以解决您的问题(在其他地方也使用 \scantoken,因此它可能会再次出现):
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{babel}
\makeatletter
\def\tikz@@parse@label@nonactive[#1]#2:#3:\pgf@nil{%
\tikzset{%
append after command = {%
\bgroup
[current point is local=true]
\pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikz@node@is@a@labelfalse}
node [every label,
tikz@label@angle = #2,
anchor=@auto,
#1,
tikz@label@post = \tikz@label@distance] {\iftikz@handle@active@nodes\expandafter\scantokens\else\expandafter\pgfutil@firstofone\fi{#3\noexpand}}
\pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}
\egroup}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
every label/.style = {label distance=1mm, inner sep=0mm,
font=\footnotesize\sffamily, align=center,
text=teal!60!black,
fill=yellow,
},
dot/.style = {circle, fill=black, inner sep=0mm, outer sep=0mm,
minimum size=1mm,
node contents={}},
]
% axes
\draw[->] (-1.7,0) -- (2.4,0) node[fill=cyan,below left] {$\phi_1(t)$};
\draw[->] (0,-1.7) -- (0,2.4) node[fill=cyan,below left] {$\phi_2(t)$};
\draw[densely dashed,very thin] (0,0) circle (13mm);
% signals
\foreach \i [count=\ix from 1] in {0,90,180,270}
\draw (\i:13mm) node[dot,label={[fill=yellow]\i+45:$s_{\ix}$}];
%----------------
\end{tikzpicture}
\end{document}
看https://tex.stackexchange.com/a/9829/2388有关 \noexpand 的讨论。