TikZ 表格文本对齐

TikZ 表格文本对齐

我在对齐 TikZ 表格中的文本时遇到了麻烦。尽管 align=left,但目前它只是将中间字母居中。

我也尝试过使用:

row 1/.style={nodes={align=left}}

(如建议使用 TikZ 矩阵创建表格

表格文本对齐

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

\tikzset
  { 
    table/.style={matrix of nodes,
                  row sep=-\pgflinewidth,
                  column sep=-\pgflinewidth,
                  nodes={rectangle,
                         draw=black,
                         text width=2ex,
                         align=left,
                         minimum width=1.5cm
                        },
                  text depth=0.25ex,
                  text height=2ex,
                  nodes in empty cells
                 },
    texto/.style={font=\footnotesize\sffamily},
    title/.style={font=\small\sffamily},
    my top text/.style={font=\small\sffamily},
    my bottom text/.style={font=\sffamily\footnotesize}
  }

\newcommand\CellText[2]{%
  \node[texto,left=of mat#1,anchor=east]
  at (mat#1.west)
  {#2};
}

\newcommand\SlText[2]{\node[my top text,anchor=base] at ($(mat#1.north)+(0,1.0ex)$) {#2};}
\newcommand\SIundertext[2]{\node[my bottom text,anchor=base] at ($(mat#1.south west)-(0,2.0ex)$) {#2};}

\newcommand\RowTitle[2]{%
\node[title,left=6.3cm of mat#1,anchor=west]
  at (mat#1.north west)
  {#2};
}
\begin{document}

\begin{tikzpicture}[node distance=0pt and 0.5cm]

  \matrix[table] (mat11) {  1,53& |[draw=none,alias=gap A]| & 22 & 22 & 22,24  & |[draw=none,alias=gap B]| & 1 \\ };

  \foreach \x/\y in {1/$0$,
                     3/$h-1$,
                     4/$h$,
                     5/$h+1$,
                     7/$b$}
  { \SlText{11-1-\x}{\y} }

  \foreach \x/\y in {1/$0$,
                     2/$\epsilon$,
                     3/$(h-2)\epsilon$,
                     4/$(h-1)\epsilon$,
                     5/$h\epsilon$,
                     6/$(h+1)\epsilon$,
                     7/$\xi\epsilon$}
    { \SIundertext{11-1-\x}{\y} }
    \node at (mat11-1-2.center) {$\ldots$};
    \node at (mat11-1-6.center) {$\ldots$};
    \node[my bottom text] at ($(mat11-1-7.south east)-(0,1.5ex)$) { $(\xi+1)\epsilon$ };

  %% create ragged edges for ellipsis in time line
  \coordinate (intrusion for box)     at (1.75ex,   0  );
  \coordinate (vertical border adj)   at (  0  , 0.2pt);
  \coordinate (horizontal border adj) at (0.2pt,   0  );

  \foreach \x in {A,B}{
  \draw ($(gap \x.south west)+(intrusion for box)+(vertical border adj)$)     -- 
        ($(gap \x.south west)+(horizontal border adj)+(vertical border adj)$) --
        ($(gap \x.north west)+(horizontal border adj)-(vertical border adj)$) --
        ($(gap \x.north west)+(intrusion for box)-(vertical border adj)$);                                                                                                                   
  \draw [decorate,decoration={zigzag,segment length=4pt,amplitude=2pt}]                                                                                                                      
        ($(gap \x.north west)+(intrusion for box)-(vertical border adj)$) --
        ($(gap \x.south west)+(intrusion for box)+(vertical border adj)$);

  \draw ($(gap \x.south east)-(intrusion for box)+(vertical border adj)$)     -- 
        ($(gap \x.south east)-(horizontal border adj)+(vertical border adj)$) --
        ($(gap \x.north east)-(horizontal border adj)-(vertical border adj)$) --
        ($(gap \x.north east)-(intrusion for box)-(vertical border adj)$);
  \draw [decorate,decoration={zigzag,segment length=4pt,amplitude=2pt}]                                                                                                                      
        ($(gap \x.north east)-(intrusion for box)-(vertical border adj)$) --
        ($(gap \x.south east)-(intrusion for box)+(vertical border adj)$);
  }

  \node[my top text,anchor=base east]    at ($(mat11-1-1.north west)-(2ex,0)+(0,1.0ex)$) {Bin};
  \node[my bottom text,anchor=base east] at ($(mat11-1-1.south west)-(2ex,0)-(0,2.0ex)$) {Time};
  \node[title,anchor=east]          at ($(mat11-1-1.west)-(1.5cm,0)$) {Vehicle $k$};

\end{tikzpicture}
\end{document}

之前的一个问题帮助了我很多,可以在这里找到带有浮动标签+时间不连续性的TikZ表

答案1

2ex太短,无法进行任何对齐。如果我使用

text width=1.5cm,

table风格上,我得到

在此处输入图片描述

这样就有足够的空间让文本以不同的方式对齐

相关内容