当 tikz 中的文本溢出时,居中对齐不起作用

当 tikz 中的文本溢出时,居中对齐不起作用

下面有一个例子。我试图强制文本位于节点的中心。

\documentclass[tikz]{standalone}
\usepackage{mwe} % For dummy images 
\usepackage{mathtools}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}[node distance=5mm and 5mm,
    input_circle/.style={
        draw,
        ellipse,
        minimum size=2cm,
        minimum height=1cm,
        text width=1cm,
        fill=white!30!yellow,
        font=\tiny,
        align=center
    }]
    \node (r_t_k)   [input_circle]                      {$r_{t-K+1}$ \\ $\log[r_{t-K+1}^2]$};
    \node (r_t_k_1) [input_circle,right=of r_t_k]       {$r_{t-K+2}$ \\ $\log[r_{t-K+2}^2]$};
    \node (dots)    [minimum size=2cm,right=of r_t_k_1] {\huge $\cdots$};
    \node (r_t_1)   [input_circle,right=of dots]        {$r_{t-1}$ \\ $\log[r_{t-1}^2]$};
    \node (r_t)     [input_circle,right=of r_t_1]       {$r_t$ \\ $\log[r_t^2]$};
\end{tikzpicture}
\end{document}

align=center 似乎会破坏溢出的文本。有没有办法强制文本居中? 在此处输入图片描述

答案1

不要覆盖自然text width

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
node distance=5mm and 5mm,
input_circle/.style={
draw,
ellipse,
minimum size=2cm,
minimum height=1cm,
fill=white!30!yellow,
font=\tiny,
align=center
}]
\node[input_circle] (r_t_k) {$r_{t-K+1}$ \\ $\log[r_{t-K+1}^2]$};
\node[input_circle, right=of r_t_k] (r_t_k_1) {$r_{t-K+2}$ \\ $\log[r_{t-K+2}^2]$};
\node[minimum size=2cm, right=of r_t_k_1] (dots) {\huge $\cdots$};
\node[input_circle, right=of dots] (r_t_1) {$r_{t-1}$ \\ $\log[r_{t-1}^2]$};
\node[input_circle, right=of r_t_1] (r_t) {$r_t$ \\ $\log[r_t^2]$};
\end{tikzpicture}
\end{document}

四个带公式的椭圆

答案2

  • 您的 elpse 样式定义不一致:
    • minimum size2=cm被覆盖 minimum height=1cmtext width=1cm对于节点中的文本来说太小了
    • 更明智的做法是定义 text width=15mm,,,minimum height=9mmalign=center
  • 无关:
    • 对于节点的定位我将使用chains
    • 为了使数学看起来更美观,我会增加字体大小
\documentclass[border=3.141592]{standalone}
\usepackage{tikz} 
\usepackage{mathtools}
\usetikzlibrary{positioning}
\usetikzlibrary{calc, chains, 
                positioning,
                shapes.geometric}

\begin{document}

    \begin{tikzpicture}[
node distance = 5mm and 5mm,
  start chain = going right,
    IC/.style = {ellipse, draw, fill=yellow!30, % input_circle
        text width=15mm, minimum height=9mm, align=center,            % < ---
        inner xsep=0pt, font=\scriptsize}
                    ]
    \begin{scope}[nodes={on chain}]
\node (r_t_k)   [IC]    {$r_{t-K+1}$  $\log\Bigl[r_{t-K+1}^2\Bigr]$};
\node (r_t_k_1) [IC]    {$r_{t-K+2}$ $\log\Bigl[r_{t-K+2}^2\Bigr]$};
\node (dots)    [font=\huge]    {$\cdots$};
\node (r_t_1)   [IC]    {$r_{t-1}$  $\log\Bigl[r_{t-1}^2\Bigr]$};
\node (r_t)     [IC]    {$r_t$\\ $\log\Bigl[r_t^2\Bigr]$};
    \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容