如何垂直居中 tikz 矩阵的元素

如何垂直居中 tikz 矩阵的元素

假设我制作了这张表:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\raggedright
\setlength{\parindent}{0.5in}
\begin{document}

\begin{center}
\begin{tikzpicture}
\matrix
  {\hline;
   [20mm] \node{Compute 1}; &[20mm] \node{}; &[20mm] \node{Compute 2};  &[20mm] \node{};  \\
  \hline \\
  \node(January){January}; & \node(February){February} ; & \node(September){September} ; & \node(December){December} ; & \\ 
   \hline \\
   };
  \draw(-7.24,-.68)--(-7.24,.68); 
  \draw(7.24,-.68)--(7.24,.68); 
  \draw(0,-.68)--(0,0);
  \draw(-3.6,-.68)--(-3.6,.68);
  \draw(3.6,-.68)--(3.6,0);
\node(round1)[below left=1cm of January]{Text 1};   
     \draw (-6.20,-0.68)--(round1);
     \node(round2)[below=1cm of February]{Text 2};   
     \draw (-2.3,-0.68)--(round2);
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

毫不奇怪,我是 tikz 的新手。我的主要问题是如何将文本居中放在两行中?换句话说,当前January向左对齐,但December向右对齐。我希望它们都居中。 和Compute 1也一样Compute 2

我绝对不会拘泥于目前表格的代码或样式,因此如果有更好的方法来创建更好看或编码更好的表格,我愿意倾听。我只需要在表格中显示诸如Text 1和 之类的行Text 2。我如何实现这一点并不重要。

答案1

像这样?

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc, matrix, positioning}

\begin{document}
    \begin{center}
\begin{tikzpicture}
\matrix (M) [matrix of nodes,
             nodes in empty cells,
             nodes={text width=22mm, text height=1em, text depth=0.25ex,
                    align=center, inner sep=2mm},
             ]
{
Compute 1   &               &   Compute 2   &               \\
January     &   February    &   September   &   December    \\
};
\draw
% horizontal lines
       (M-1-1.north west) -- (M-1-4.north east)
        (M-1-1.south west) -- (M-1-4.south east)
        (M-2-1.south west) -- (M-2-4.south east)
% vertical lines
        (M-1-1.north west) -- (M-2-1.south west)
        (M-1-1.north east) -- (M-2-1.south east)
        (M-2-2.north east) -- (M-2-2.south east)
        (M-2-3.north east) -- (M-2-3.south east)
        (M-1-4.north east) -- (M-2-4.south east)
        ;
\draw   (M-2-1.south) -- + (-1,-1) node[below]  {Text 1};
\draw   (M-2-2.south) -- + ( 1,-1) node[below]  {Text 2};
\end{tikzpicture}
    \end{center}
\end{document}

上面的 MWE 与您的 MWE 相比有以下变化:

  • 定义为节点矩阵
  • 空矩阵单元被视为空节点
  • 对于节点,定义通用样式,其中定义节点内容居中
  • “表格”的所有线条都是单独绘制的

答案2

我建议使用nodes 的更简单的方法。

\documentclass[12pt,border=5pt,tikz]{standalone}
\begin{document}

\begin{tikzpicture}[rec/.style={draw,minimum width=2.5cm,minimum height=1cm},outer sep=0pt]
\node (1) [rec] {Compute 1};
\node (2)   at (1.east)  [rec,anchor=west,minimum width=7.5cm] {Compute 2};
\node (jan) at (1.south) [rec,anchor=north] {January};
\node (feb) at (jan.east)[rec,anchor=west] {February};
\node (sep) at (feb.east)[rec,anchor=west] {September};
\node (dec) at (sep.east)[rec,anchor=west] {December};
\draw [below] (jan.south) -- ++(-1,-1)node{Text 1} (feb.south) -- ++(1,-1)node{Text 2};
\end{tikzpicture}

\end{document}

在此处输入图片描述

极端一点来说,上述想法可以通过循环实现,如下所示:

\documentclass[border=5pt,tikz]{standalone}
\begin{document}

\begin{tikzpicture}[rec/.style={draw,minimum height=.7cm,anchor=west,outer sep=0pt}]    
\foreach \pair/\txt[count=\n] in {{0,.7}/Compute 1,
                                  {2,.7}/Compute 2,
                                  {0,0}/January,
                                  {2,0}/February,
                                  {4,0}/September,
                                  {6,0}/December}
  \node (\txt) at (\pair) [rec,minimum width={\ifnum\n=2 6cm\else2cm\fi}]{\txt};    
\draw [below] (January.south)--++(-.5,-.5)node{Text 1} (February.south)--++(.5,-.5)node{Text 2};    
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

我将在小页面中使用表格并在 tikzpicture 中执行此操作......

检查代码:

    \documentclass[12pt]{article}
    \usepackage{tikz}
    \usepackage{array}
    
    
    \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
    
    \begin{document}
    
    \begin{center}
    \begin{tikzpicture}
    \node at (0,0) {
    \begin{minipage}{15cm}
     \begin{tabular}{|*{4}{C{3.1cm}|} }
     \hline
     Compute 1 & \multicolumn{3}{c|}{Compute 2}\\\hline
     January&February&September&December\\\hline
     \end{tabular}
    \end{minipage}
    };
    \draw[thick,-latex] (-5.7,-.5)--(-7.3,-2) coordinate (Text1);
    \draw[thick,-latex] (-2.4,-.5)--(-1.2,-2) coordinate (Text2);
    \node[below left] at(Text1){Text 1};
    \node[below ] at(Text2){Text 2};
    
    \end{tikzpicture}
    \end{center}

    \end{document}

结果如下: 在此处输入图片描述

我认为这是一种清晰而简单的方法,在处理位置等方面有很好的选择......

我认为您不必为此使用矩阵。

答案4

环境中的简单表格postcript(用于auto-pst-pdf)和一些nodes来自 node connectionspst-node简短代码:

\documentclass{article}
\usepackage{fourier, erewhon}
\usepackage{array}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{center}\renewcommand\arraystretch{1.5}
  \begin{postscript}
    \begin{tabular}{|*{4}{>{\centering}p{2.5cm}|}}
      \hline
      Compute 1 & \multicolumn{3}{c|}{Compute 2} \\
      \hline
      \rnode{J}{January} & \rnode{F}{February} & September & December \tabularnewline
      \hline
    \end{tabular}
    \psset{nodesepA=3pt, linewidth=0.5pt, labelsep=1.3cm}
    \pnode[0pt, -1.7ex](J){Ju}
    \nput*{-150}{J}{\rnode{TJ}{Text 1}}\nput*{-90}{F} {\rnode{TF}{Text 2}}
    \ncline[nodesepB=0pt]{TJ}{Ju}\ncline[nodesepB=0.7ex]{TF}{F}
  \end{postscript}
\end{center}

\end{document} 

在此处输入图片描述

相关内容