2

Can anyone explain why this simple table comes out skewed?

\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}

enter image description here

2

You can for coordinates for vertical lines consider east sides of nodes in the first row. With this approach you not need to define node widths (assuming, that the nodes in the first row are the widest):

\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}             

enter image description here

Addendum: In case that you like to define minimum width to all nodes, but different in some column, than you can define the most used width in nodes style definition, and exceptional with, for example column 6/.style={nodes={minimum width=8cm} (for column 6) as you noted in your comment below. In this case is not need to define vertical lines in respect to nodes width in the first row. An example of the such solution is:

\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}

enter image description here

  • Yes, this is nIce. But I really want to specify a wider column, independently from the first line text.... – Rmano Feb 18 '17 at 23:23
  • Found it. You have to use column 6/.style={nodes={minimum width=8cm},. – Rmano Feb 19 '17 at 13:31
  • I wonder, what is purpose of your matrix. It seems, that simple tabular environment would better fulfill your requirements. – Zarko Feb 19 '17 at 18:49
  • It was just an exercise --- I am trying to better understand matrix. I used it to propose a kind of "form" in tex.stackexchange.com/questions/354463/… – Rmano Feb 19 '17 at 19:17
1

The empty nodes are created with a minimum width of 1 cm, as you request. But the title Frequency is larger than that.

Change to nodes = {minimum width=2cm,} in order to see the difference.

The setting column 4/.style={minimum width=8cm} does not have any effect, as the column itself does not have a width. However, you could say column 4/.style={text width=8cm} to achieve what you initially wanted.

  • yes, but I am saying that nodes in column 4 has 8cm minimum width... – Rmano Feb 18 '17 at 20:03
  • see updated answer :) – Philipp Imhof Feb 18 '17 at 20:03
  • Yep - I see. So how can I have a column wider than the others? – Rmano Feb 18 '17 at 20:06
  • I was wrong saying that the style did not have any effect. Answer updated (again). I should have checked more thoroughly. – Philipp Imhof Feb 18 '17 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.