更改矩形内的字体大小时行距错误

更改矩形内的字体大小时行距错误

我尝试更改矩形内的字体大小,但最后一行和倒数第二行之间的行距总是不对。该如何修复?

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{block} = [rectangle, draw, text centered, thick, node distance = 2cm, text width=1cm]   
    \node[block]                (init)  {1 \\  2 \\ 3  \\ 4};
    \node[block,right of=init]  (second){\scriptsize 1 \\ 2 \\ 3  \\ 4};
    \node[block,right of=second](third) {1 \\ \scriptsize 2 \\ 3  \\ 4};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

有效的解决方法。当更改整个节点的字体大小时,我font更喜欢使用选项。

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{block} = [rectangle, draw, text centered, thick, node distance = 2cm, text width=1cm]   
    \node[block]                (init)  {1 \\  2 \\ 3  \\ 4};
    \node[block,right of=init,font=\scriptsize]  (second){1 \\ 2 \\ 3 \\ 4};
    \node[block,right of=second,font=\scriptsize](third) {{\normalsize 1} \\ 2 \\ 3 \\ 4};
\end{tikzpicture}
\end{document}

答案2

将行放入tabular

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand{\mylist}[2][]{#1\begin{tabular}{@{}c@{}}#2\end{tabular}}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{block} = [rectangle, draw, text centered, thick, 
                         node distance = 2cm, text width=1cm]
    \node[block]                (init)  {\mylist{1 \\  2 \\ 3  \\ 4}};
    \node[block,right of=init]  (second){\mylist[\scriptsize]{1 \\ 2 \\ 3  \\ 4}};
    \node[block,right of=second](third) {\mylist[\scriptsize]{\normalsize 1 \\ 2 \\ 3  \\ 4}};
\end{tikzpicture}
\end{document}

可选参数是“全局”大小,您可以为单行覆盖它。

答案3

我更喜欢 Torbjørn T. 的解决方案,但我想了解为什么该选项font有效?

另一种可能性是

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{block} = [rectangle, draw, text centered, thick, 
    node distance = 2cm, 
    text width=1cm]   
    \node[block]                (init)  {1 \\  2 \\ 3  \\ 4};
    \node[block,right of=init]  (second){{\scriptsize 1} \\{\normalsize 2}\\{\scriptsize 3}\\{\normalsize4 }\vfill};
    \node[block,right of=second](third) {1 \\ \scriptsize 2 \\ 3  \\ 4\vfill};
\end{tikzpicture} 

\end{document} 

相关内容