我正在尝试使用 tikz 在一些文本中绘制一组彩色球,但我很难将节点与文本对齐。这是一个最小的工作示例:
\documentclass{standalone}
\usepackage{tikz,xifthen}
\begin{document}
This is a set of colored balls:
\{
\tikz[baseline]{%
\foreach \x/\color in {1/red,2/white,3/blue} {
\node[circle,draw,fill=\color,line width=1pt,minimum size=0.25em,anchor=base] at (\x,0) {};
\ifthenelse{\NOT 1 = \x}{\node at ({\x-0.5},0) {,};}{}
}
}
\}.
\end{document}
这将产生以下输出:
我想让圆圈垂直居中,包含文本/数学。我尝试了各种锚点选项,但也许我忽略了一些非常简单的东西。任何指点都将不胜感激。
答案1
\documentclass[border=1cm]{standalone}
\usepackage{tikz, xifthen}
\begin{document}
This is a set of colored balls:
\{, %, inserted to show baseline
\begin{tikzpicture}[baseline]
\foreach \x/\color in {1/red,2/white,3/blue} {
\node[circle, draw, fill=\color, line width=1pt, minimum size=0.25em, anchor=south] at (\x,0) {};
\ifthenelse{\NOT 1 = \x}{\node[anchor=base] at ({\x-0.5},0) {,};}{}
}
\end{tikzpicture}
,\}. %, inserted to show baseline
\end{document}
答案2
在评论的帮助下,我通过计算正确的坐标找到了解决方案:使用 0.1em 的边框和 0.25em 的半径,中心位于 (0.25 + 0.1 + 0.1)/2 = 0.225,因此我可以将圆圈定位在特定的 y 坐标处并锚定到基座。
\documentclass{standalone}
\usepackage{tikz,xifthen}
\begin{document}
This is a set of colored balls:
\{
\tikz[baseline]{%
\foreach \x/\color in {1/red,2/white,3/blue} {
\node[circle,draw,fill=\color,line width=0.1em,minimum size=0.25em,anchor=base] at (\x,0.225em) {};
\ifthenelse{\NOT 1 = \x}{\node at ({\x-0.5},0) {,};}{}
}
}
\}.
\end{document}
答案3
通过这篇文章如何使 TikZ 节点在文本行内垂直居中?和Àpprendre à programmer en tex
ctan p. 259
\begingroup
\setbox0\hbox{$\vcenter{}$}%
\xdef\htmath{\the\ht0}%
\endgroup
给出数学轴的垂直偏移。
使用 expl3,我们准备一个序列来放置命令tikz
,然后使用“,”分隔该序列。
\documentclass{article}
\usepackage{tikz}
%%%%%%%%%%%%%%%
\tikzset{%
ball/.style args={#1}{%
circle,
draw,
fill=#1,
line width=1pt,
minimum size=0.25em,
node contents={},
}
}
\ExplSyntaxOn
\clist_new:N \l_modulename_color_clist
\seq_new:N \l_modulename_balls_seq
\NewDocumentCommand{\myballs}{ O{red,white,blue} }
{
% in the doc Apprendre à programmer en tex p.259
\begingroup
\setbox0\hbox{$\vcenter{}$}%
\xdef\htmath{\the\ht0}%
\endgroup
%The~mathematical~axis:~\htmath\par
%%%%%%%%%%%%%%%%%
\clist_set:Nn \l_modulename_color_clist {#1}
\seq_clear:N \l_modulename_balls_seq
\clist_map_inline:Nn \l_modulename_color_clist
{
\seq_put_right:Nn \l_modulename_balls_seq {\tikz[baseline=-\htmath]\node [ball=##1];}
}
\seq_use:Nn \l_modulename_balls_seq {,~}
}
\ExplSyntaxOff
\begin{document}
This is a set of colored balls: \myballs
\Huge
and in with \verb|\Huge|: \myballs[yellow,green,black,purple]
\end{document}