Tikz 中节点文本的垂直对齐

Tikz 中节点文本的垂直对齐

有人能告诉我为什么文本是顶部对齐的,例如字母将文本向下移动,但o仍然粘在顶部,从而导致两个节点错位?

我希望文本垂直居中,两个字母,o处于同一高度。

代码由@JasperHabicht 提供: https://tex.stackexchange.com/a/711612/262813

我尝试调整设置并得出以下结论:

[baseline=(a), >=stealth,每个节点/.style={最小高度=10ex,文本深度=10ex,align=center}]`

但没有成功。


\documentclass[tikz, border=10pt]{standalone}
\usepackage{tikz}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usetikzlibrary{decorations.pathreplacing, fit}

\ExplSyntaxOn

\cs_generate_variant:Nn \seq_set_split:Nnn { Nee }
\seq_new:N \l_vincentkrebs_coolsource_deriv_parts_seq
\cs_new:Npn \vincentkrebs_coolsource_format #1 {
    \seq_set_split:Nee \l_vincentkrebs_coolsource_deriv_parts_seq { , } { #1 }
    \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_deriv_parts_seq { 1 } }
    \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_deriv_parts_seq { 2 } } 
    \textit{\tl_use:N \l_tmpa_tl} \tl_if_empty:NF \l_tmpb_tl {\textup{\textsubscript{\textsc{\tl_use:N \l_tmpb_tl}}}}
}

\seq_new:N \l_vincentkrebs_coolsource_derivs_seq
\clist_new:N \l_vincentkrebs_coolsource_fitspec_clist


\NewDocumentCommand{\coolsource}{ o O{} m }{
    \seq_set_split:Nnn \l_vincentkrebs_coolsource_derivs_seq { ; } { #3 }
    \clist_clear:N \l_vincentkrebs_coolsource_fitspec_clist
    \begin{tikzpicture}[baseline=(a), >=stealth,every node/.style={minimum height=10ex,text depth=10ex,align=center}]
        \coordinate (a) at (0,0);
        \IfValueT{#1}{
            \node[anchor=south, #2] (A) at (a) { \vincentkrebs_coolsource_format { #1 } };
            \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (A) }
        }
        \coordinate (b) at (-1em,-0.6em);
        \coordinate (c) at (1em,-0.6em);
        \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 1 } }
        \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 2 } }
        \node[anchor=east] (B) at (b) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };
        \node[anchor=west] (C) at (c) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpb_tl } };
        \draw[<->] (b.west) -- (a.south) -- (c.east);
        \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (B), (C) }
        
        \int_compare:nNnT { \seq_count:N \l_vincentkrebs_coolsource_derivs_seq } > { 2 } {
            \int_compare:nNnTF { \seq_count:N \l_vincentkrebs_coolsource_derivs_seq } > { 3 } {
                \coordinate (d) at (0em,-1.2em);
                \coordinate (e) at (0em,-1.2em);
                \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 3 } }
                \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 4 } }
                \node[anchor=north ~ east] (D) at (d) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };
                \node[anchor=north ~ west] (E) at (e) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpb_tl } }; 
                \draw[->] (a.south) -- ([xshift=-0.5em]d.north);
                \draw[->] (a.south) -- ([xshift=0.5em]e.north);
                \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (D), (E) }
            } {
                \coordinate (d) at (0em,-1.2em);
                \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 3 } }
                \node[anchor=north] (D) at (d) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };  
                \draw[->] (a.south) -- (d.north);
                \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (D) }
            }
        } 
        
        \node[fit={ \clist_use:Nn \l_vincentkrebs_coolsource_fitspec_clist { } }] (frame) {};
        \draw[decorate, decoration={brace, mirror}] (frame.north ~ west) -- (frame.south ~ west);
        \draw[decorate, decoration={brace}] (frame.north ~ east) -- (frame.south ~ east);
    \end{tikzpicture}
}

\ExplSyntaxOff

\begin{document}
    \coolsource{ion,n;ory,n;i,n;i,n}
    
    
    \coolsource{ion,n;ory,n;o,n;o,n}
    
    
    \coolsource{ion,n;ory,n;i,n;o,n}
    
    
    \coolsource{ion,n;ory,n;o,n;i,n}
    
    
\end{document}

在此处输入图片描述

编辑

经过@cfr的修正,(A)周围的可选形状被锚定在低位:

\begin{document}
    
    
    \coolsource[source,v][draw]{ion,n;ory,n;i,n;i,n}
    
    
    
\end{document}

在此处输入图片描述

编辑

解决办法就是anchor=south保持节点(A)的设置

答案1

我无法按原样编译您的示例,因此我删除了自定义字体设置。要将节点与基线对齐,请使用basebase westbase east。我猜这就是您想要的,但是,如果您确实希望它居中,请使用centerwesteast

与基节点对齐的节点

\documentclass[tikz, border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing, fit}

\ExplSyntaxOn

\cs_generate_variant:Nn \seq_set_split:Nnn { Nee }
\seq_new:N \l_vincentkrebs_coolsource_deriv_parts_seq
\cs_new:Npn \vincentkrebs_coolsource_format #1 {
    \seq_set_split:Nee \l_vincentkrebs_coolsource_deriv_parts_seq { , } { #1 }
    \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_deriv_parts_seq { 1 } }
    \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_deriv_parts_seq { 2 } } 
    \textit{\tl_use:N \l_tmpa_tl} \tl_if_empty:NF \l_tmpb_tl {\textup{\textsubscript{\textsc{\tl_use:N \l_tmpb_tl}}}}
}

\seq_new:N \l_vincentkrebs_coolsource_derivs_seq
\clist_new:N \l_vincentkrebs_coolsource_fitspec_clist


\NewDocumentCommand{\coolsource}{ o O{} m }{
  \seq_set_split:Nnn \l_vincentkrebs_coolsource_derivs_seq { ; } { #3 }
  \clist_clear:N \l_vincentkrebs_coolsource_fitspec_clist
  \begin{tikzpicture}[baseline=(a), >=stealth]
    \coordinate (a) at (0,0);
    \IfValueT{#1}{
      \node[anchor=south, #2] (A) at (a) { \vincentkrebs_coolsource_format { #1 } };
      \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (A) }
    }
    \coordinate (b) at (-1em,-0.6em);
    \coordinate (c) at (1em,-0.6em);
    \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 1 } }
    \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 2 } }
    \node[anchor=base ~ east] (B) at (b) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };
    \node[anchor=base ~ west] (C) at (c) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpb_tl } };
    \draw[<->] (b.west) -- (a.south) -- (c.east);
    \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (B), (C) }
    
    \int_compare:nNnT { \seq_count:N \l_vincentkrebs_coolsource_derivs_seq } > { 2 } {
      \int_compare:nNnTF { \seq_count:N \l_vincentkrebs_coolsource_derivs_seq } > { 3 } {
        \coordinate (d) at (0em,-1.2em);
        \coordinate (e) at (0em,-1.2em);
        \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 3 } }
        \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 4 } }
        \node[anchor=north ~ east] (D) at (d) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };
        \node[anchor=base ~ west] (E) at (e |- D.base) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpb_tl } }; 
        \draw[->] (a.south) -- ([xshift=-0.5em]d.north);
        \draw[->] (a.south) -- ([xshift=0.5em]e.north);
        \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (D), (E) }
      } {
        \coordinate (d) at (0em,-1.2em);
        \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_vincentkrebs_coolsource_derivs_seq { 3 } }
        \node[anchor=north] (D) at (d) { \vincentkrebs_coolsource_format { \tl_use:N \l_tmpa_tl } };  
        \draw[->] (a.south) -- (d.north);
        \clist_push:Nn \l_vincentkrebs_coolsource_fitspec_clist { (D) }
      }
    } 
    
    \node[fit={ \clist_use:Nn \l_vincentkrebs_coolsource_fitspec_clist { } }] (frame) {};
    \draw[decorate, decoration={brace, mirror}] (frame.north ~ west) -- (frame.south ~ west);
    \draw[decorate, decoration={brace}] (frame.north ~ east) -- (frame.south ~ east);
  \end{tikzpicture}
}

\ExplSyntaxOff

\begin{document}
    \coolsource{ion,n;ory,n;i,n;i,n}
    
    
    \coolsource{ion,n;ory,n;o,n;o,n}
    
    
    \coolsource{ion,n;ory,n;i,n;o,n}
    
    
    \coolsource{ion,n;ory,n;o,n;i,n}
    
    
\end{document}

我不清楚expl3在这里使用 对你有多大帮助。请注意,此语法不一定与 pgf/tikz 语法兼容,因此你完全有可能遇到冲突。话虽如此,我也将它们混合使用 - 只是你需要对潜在的陷阱稍微警惕一点。

相关内容