tikz中的矩阵问题

tikz中的矩阵问题

有人能解释一下为什么这个简单的表格会歪斜吗?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[
    ]
    \matrix (m) [matrix of nodes, nodes in empty cells,
        nodes = {minimum width=1cm,},    
        column 4/.style={minimum width=8cm},
    ]
    {
        Sr  & Drug & Dose & Frequency & Duration & Remark \\ 
        1  &      &      &           &          &        \\ 
        2  &      &      &           &          &        \\ 
        3  &      &      &           &          &        \\ 
        4  &      &      &           &          &        \\ 
        5  &      &      &           &          &        \\ 
        6  &      &      &           &          &        \\ 
        7  &      &      &           &          &        \\ 
    };

    \foreach \c in {1,...,6} 
    \draw [gray] (m-1-\c.north east) --  (m-8-\c.south east);

    \draw (m-1-1.south west) -- (m-1-6.south east);
    \draw (m-8-1.south west) -- (m-8-6.south east);

    \foreach \r in {2,...,7} 
    \draw [dotted, blue] (m-\r-1.south west) --  (m-\r-6.south east);


\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

对于垂直线的坐标,您可以考虑第一行节点的东侧。使用这种方法,您不需要定义节点宽度(假设第一行的节点最宽):

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
    nodes = {minimum width=1cm,minimum height=1ex,text depth=0.25ex},
%        column 4/.style={minimum width=8cm},
]
{
    Sr  & Drug & Dose & Frequency & Duration & Remark \\
    1  &      &      &           &          &        \\
    2  &      &      &           &          &        \\
    3  &      &      &           &          &        \\
    4  &      &      &           &          &        \\
    5  &      &      &           &          &        \\
    6  &      &      &           &          &        \\
    7  &      &      &           &          &        \\
};

\foreach \c in {1,...,6}
\draw [gray] (m-1-\c.north east) --  (m-1-\c.north east |- m-8-1.south);

\draw (m-1-1.south west) -- (m-1-6.south east);
\draw (m-8-1.south west) -- (m-8-6.south east -| m-1-6.east);

\foreach \r in {2,...,7}
\draw [dotted, blue] (m-\r-1.south west) --  (m-\r-1.south west -| m-1-6.east);
    \end{tikzpicture}
\end{document}             

在此处输入图片描述

附录: 如果您希望为所有节点定义最小宽度,但在某些列中有所不同,那么您可以在节点样式定义中定义最常用的宽度,并进行例外处理,例如column 6/.style={nodes={minimum width=8cm}(对于第 6 列),如您在下面的评论中所述。在这种情况下,不需要在第一行中根据节点宽度定义垂直线。此类解决方案的一个示例是:

\documentclass[tikz, margin=3mm, preview]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
    nodes = {minimum width=22mm, inner xsep=2mm, minimum height=1ex,text depth=0.25ex},
    column 1/.style={nodes={minimum width= 8mm}},
    column 6/.style={nodes={minimum width=88mm}}
            ]
{
    Sr 
        & Drug 
            & Dose 
                & Frequency 
                    & Duration 
                        & Remark \\
    1   &   &   &   &   &        \\
    2   &   &   &   &   &        \\
    3   &   &   &   &   &        \\
    4   &   &   &   &   &        \\
    5   &   &   &   &   &        \\
    6   &   &   &   &   &        \\
    7   &   &   &   &   &        \\
};

\draw [gray] (m-1-1.north west) --  (m-8-1.south west);
\foreach \c in {1,...,6}
\draw [gray] (m-1-\c.north east) --  (m-8-\c.south east);

\draw (m-1-1.north west) -- (m-1-6.north east);
\draw (m-1-1.south west) -- (m-1-6.south east);
\draw (m-8-1.south west) -- (m-8-6.south east);

\foreach \r in {2,...,7}
\draw [dotted, blue] (m-\r-1.south west) --  (m-\r-6.south east);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

空节点由minimum width。但标题频率比那更大。

更改为nodes = {minimum width=2cm,}才能看到差异。

该设置column 4/.style={minimum width=8cm}没有任何效果,因为列本身没有宽度。不过,可以说column 4/.style={text width=8cm}实现了你最初想要的效果。

相关内容