14

In tablular, we use {r|c|l} to define alignment. Is there anything similar for Tikz matrix. For example, by using matrix, the following MWE produces matrix of nodes, which appear to be center aligned for each column.

However, I would like to have the nodes in the first column right-aligned, the middle column center-aligned, while the third column left-aligned. How would I achieve this? (also note, the nodes may have different styles)

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[align=center]
\matrix[matrix of nodes,row sep=0.5cm,column sep=2cm,
    nodes={rectangle, draw}]{
col1 & col2 & col3 \\
col111111111 & col2222222222 & col33333\\
col1111 & col22222 & col3333333333 \\
};

\end{tikzpicture}
\end{document}

yields this:

enter image description here

1 Answer 1

28

Tikz matrices define the styles column X which allows to pass separate options (in this case, anchor) for different columns. For the record, the row X styles are the equivalent for rows, and you can combine both to refer to a specific cell.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[align=center]
  \matrix[matrix of nodes,row sep=0.5cm,column sep=2cm,
    nodes={rectangle, draw},
    column 1/.style={anchor=base east},
    column 2/.style={anchor=base},
    column 3/.style={anchor=base west}
  ]{
    col1 & col2 & col3 \\
    col111111111 & col2222222222 & col33333\\
    col1111 & col22222 & col3333333333 \\
  };

\end{tikzpicture}
\end{document}

enter image description here

More information is available in the pgfmanual (section 17.3.3).

2
  • is there a convenient way to apply a column style to all columns? Sep 13, 2017 at 18:20
  • @BrianFitzpatrick The column style applies the style to all cells of the column. So applying a style to all columns means applying the style to all cells of the matrix, so every node for example should do it.
    – T. Verron
    Sep 13, 2017 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .