tikz 框内的文本对齐

tikz 框内的文本对齐

我尝试过垂直对齐框中的文本,但没有成功。不知何故,tikz我无法正确对齐框中的文本。提前致谢。

在此处输入图片描述

\documentclass[varwidth=true, border=1mm, 11pt]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage{color, colortbl}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds,external,shapes,arrows,positioning,calc,fit, spy}
\usepgflibrary{decorations.markings}
\usepackage[activate={true, nocompatibility},final,tracking=true,kerning=true,stretch=20,shrink=20,factor=1100]{microtype}
\begin{document}          
\tikzsetnextfilename{FPGA_1}
\begin{tikzpicture}[>=latex,text height=1.5ex,text depth=0.5ex]
% Interconnect
\tikzstyle{c}=[circle,
               thick,
               scale=0.2,
               draw=gray!100,
               fill=white!50]
% Block
\tikzstyle{d}=[rectangle,
               rounded corners=5pt,
               thick,
               text centered,
               minimum size=1cm,
               draw=gray!100,
               fill=gray!80]

\matrix[row sep=0.1cm,column sep=0.1cm] {
% First row
\node(A1)[d]{XYZ};&
\node(cA1)[c]{};&
\node(A2)[d]{XYZ};&
\\
\node(cc1)[c]{};&
\node(c)[c]{};&
\node(cc2)[c]{};&
\\
% Second row
\node(B1)[d]{XYZ};&
\node(cB1)[c]{};&
\node(B2)[d]{XYZ};&
\\
};
\begin{scope}[on background layer]
% Horizontal lines
\path[-, thick](A1) edge (A2);
\path[-, thick](cc1) edge (cc2);
\path[-, thick](B1) edge (B2);
% Vertical lines
\path[-, thick](A1) edge (B1);
\path[-, thick](cA1) edge (cB1);
\path[-, thick](A2) edge (B2);
\end{scope}
\end{tikzpicture}
\end{document}

答案1

欢迎使用 TeX-SE!文本未垂直居中的原因是您设置了text height和 ,text depth使得它们未垂直居中text height=1.5ex,text depth=0.5ex。使节点中的文本垂直居中的一种方法是将text height节点中的 重置为不同的值。另请注意 已\tikzstyle弃用。

\documentclass[varwidth=true, border=1mm, 11pt]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage{color, colortbl}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds,external,shapes,arrows,positioning,calc,fit, spy}
\usepgflibrary{decorations.markings}
\usepackage[activate={true, nocompatibility},final,tracking=true,kerning=true,stretch=20,shrink=20,factor=1100]{microtype}
\begin{document}          
\tikzsetnextfilename{FPGA_1}
\begin{tikzpicture}[>=latex,text height=1.5ex,text depth=0.5ex]
% Interconnect
\tikzset{c/.style={circle,
               thick,
               scale=0.2,
               draw=gray!100,
               fill=white!50},
% Block
d/.style={rectangle,
               rounded corners=5pt,
               thick,
               text centered,text height=0.9em,
               minimum size=1cm,
               draw=gray!100,
               fill=gray!80}}

\matrix[row sep=0.1cm,column sep=0.1cm] {
% First row
\node(A1)[d]{XYZ};&
\node(cA1)[c]{};&
\node(A2)[d]{XYZ};&
\\
\node(cc1)[c]{};&
\node(c)[c]{};&
\node(cc2)[c]{};&
\\
% Second row
\node(B1)[d]{XYZ};&
\node(cB1)[c]{};&
\node(B2)[d]{XYZ};&
\\
};
\begin{scope}[on background layer]
% Horizontal lines
\path[-, thick](A1) edge (A2);
\path[-, thick](cc1) edge (cc2);
\path[-, thick](B1) edge (B2);
% Vertical lines
\path[-, thick](A1) edge (B1);
\path[-, thick](cA1) edge (cB1);
\path[-, thick](A2) edge (B2);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果你删除所有这些文本参数(并且删除这里未使用的包和库),你会得到

\documentclass[tikz, border=1mm, 11pt]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usetikzlibrary{positioning,backgrounds}
\usepackage[activate={true, nocompatibility},final,tracking=true,kerning=true,stretch=20,shrink=20,factor=1100]{microtype}
\begin{document}          
\begin{tikzpicture}[>=latex,
c/.style={circle,
               thick,
               scale=0.2,
               draw=gray!100,
               fill=white!50},
d/.style={rectangle,
               rounded corners=5pt,
               thick,
               text centered,
               minimum size=1cm,
               draw=gray!100,
               fill=gray!80}]

\matrix[row sep=0.1cm,column sep=0.1cm] {
% First row
\node(A1)[d]{XYZ};&
\node(cA1)[c]{};&
\node(A2)[d]{XYZ};&
\\
\node(cc1)[c]{};&
\node(c)[c]{};&
\node(cc2)[c]{};&
\\
% Second row
\node(B1)[d]{XYZ};&
\node(cB1)[c]{};&
\node(B2)[d]{XYZ};&
\\
};
\begin{scope}[on background layer]
% Horizontal lines
\path[-, thick](A1) edge (A2);
\path[-, thick](cc1) edge (cc2);
\path[-, thick](B1) edge (B2);
% Vertical lines
\path[-, thick](A1) edge (B1);
\path[-, thick](cA1) edge (cB1);
\path[-, thick](A2) edge (B2);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

自动垂直居中。

答案2

作为@marmot 答案中第二个例子的补充(+1):

\documentclass[tikz, table, border=1mm, 11pt]{standalone}
\usetikzlibrary{backgrounds,
                matrix,
                positioning}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage[activate={true, nocompatibility},
            final,
            tracking=true,kerning=true,
            stretch=20,shrink=20,
            factor=1100]{microtype}

\begin{document}
    \begin{tikzpicture}[
base/.style = {draw=gray, thick,
               outer sep=0pt, anchor=center},
% Interconnect
c/.style = {circle, base,
            fill=white, inner sep=1pt},
% Block
d/.style = {base, rounded corners=5pt,
            fill=gray!80,
            minimum size=1cm}
                    ]

\matrix (m) [matrix of nodes,
             column sep=1mm,
             row sep=1mm]
{
|[d]| XYZ   & |[c]| & |[d]| XYZ \\
|[c]|       & |[c]| & |[c]|     \\
|[d]| XYZ   & |[c]| & |[d]| XYZ \\
};
    \begin{scope}[on background layer]
\foreach \i in {1,2,3}
{
% Horizontal lines
\draw[thick]    (m-\i-1) -- (m-\i-3)
% Vertical lines
                (m-1-\i) -- (m-3-\i);
}
\end{scope}
    \end{tikzpicture}
\end{document}

结果是一样的:

在此处输入图片描述

差异如下:

  • 使用的是节点矩阵
  • 节点样式的参数被最小化
  • 循环绘制线条

相关内容