4

I am trying to get what's below in order in the sense of: (a) the black text on the left (xx, yy, zz) should be vertically aligned with respect to the relevant table cell on the right; (b) there is no so much space between the left column and the right column with the boxes; (c) this is a tricky one: as you can see, in the lowest set of boxes on the right, I am trying to align D and E with B and C, but for some reason, the tikz picture gets "cropped".

I should mention I am using tabularray after finding the following answer: How to vertically-center the text of the cells?

Here is the MWE:

\documentclass{article}

\usepackage{tabularray} \usepackage{tikz}

\begin{document}

\begin{tblr}{ colspec = {X[l,m]X[l,m]}, rowsep = 0pt, colsep = 0pt }

zz: & something \

yy: & \begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]

\path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C}; \draw[->] (A)--(B); \draw[->] (B)--(C);

\end{tikzpicture} \

xx & \begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}]

\path (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E}; \draw[->] (AA)--(BB);

\end{tikzpicture}

\end{tblr}

\end{document}

  • The X stretches the column to fill a horizontal space. If you don't want that don't use it. You can use baseline=(A.base) or baseline=(AA.base) to vertically align the TikZ pictures correctly. – Qrrbrbirlbel Mar 03 '23 at 11:02

5 Answers5

5

(a) Set the baseline of tikz pictures.
(b) Use Q column type instead of X.
(c) Draw an invisible node below A to align B with D and C with E.

I have increased the box height just to demonstrate.

\documentclass{article}

\usepackage{tabularray} \usepackage{tikz} \usetikzlibrary{positioning}

\begin{document}

\begin{tblr}{
        colspec = {Q[l,m] Q[l,m]},
        rowsep = 0pt,
        colsep = 0pt
    }

    zz: & something \\

    yy: & \begin{tikzpicture}[
        cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm,minimum height=10mm},
        cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        baseline={(A.base)}
    ]

        \path  (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C};
        \draw[->] (A)--(B);
        \draw[->] (B)--(C);
        %\path node (yy) [left=of A, inner sep=0pt, rectangle, draw] {yy};

    \end{tikzpicture} \\

    xx & \begin{tikzpicture}[
        cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm,minimum height=10mm},
        cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},
        invisible/.style={inner sep=4,outer sep=0,minimum width=18mm},
        baseline={(AA.base)}
    ]


        \path (0,0) node[invisible] {} (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E};
        \draw[->] (AA)--(BB);

    \end{tikzpicture}

\end{tblr}

\end{document}

enter image description here

Zxcvasdf
  • 1,735
5
  • used arrows.meta˛ chainsandpositioning` TikZ libraries
  • common tikz style definition
  • short code ...
\documentclass{article}

\usepackage{tabularray} \usepackage{tikz} \usetikzlibrary{arrows.meta, chains, positioning}

\begin{document}

\tikzset{
node distance = 0mm and 5mm, start chain = going right, N/.style = {fill=#1!20, rounded corners=2pt, anchor=base, inner ysep=4mm, inner xsep=0mm, minimum width=18mm, on chain}, N/.default = blue, every edge/.append style = {-Straight Barb,} }

\begin{tblr}{colspec = {Q[l,m] Q[r,m, wd=64mm]}, colsep = 1mm, rowsep = 0pt}

zz: & \SetCell{l} something \ yy: & \tikz[baseline=1pt] {\node[N] (a) {A}; \node[N] (b) {B}; \node[N] (c) {C}; \draw (a) edge (b) (b) edge (c); } \ xx: & \tikz[baseline=1pt] {\node[N=yellow] (a) {D}; \node[N=green] (b) {E}; \draw (a) edge (b); } \end{tblr}

\end{document}

Edit: or a bit shorter code by use of the join macro:

\documentclass{article}

\usepackage{tabularray} \usepackage{tikz} \usetikzlibrary{arrows.meta, chains, positioning}

\begin{document}

\tikzset{ node distance = 0mm and 5mm, start chain = going right, N/.style = {fill=#1!20, rounded corners=2pt, anchor=base, inner ysep=4mm, inner xsep=0mm, minimum width=18mm, on chain}, N/.default = blue, every join/.style = {-Straight Barb} % <--- }

\begin{tblr}{colspec = {Q[l,m] Q[r,m, wd=64mm]}, colsep = 1mm, rowsep = 0pt}

zz: & \SetCell{l} something \ yy: & \tikz[baseline=1pt] {\node[N] (a) {A}; \node[N, join] {B}; % <--- \node[N, join] {C}; % <--- } \ xx: & \tikz[baseline=1pt] {\node[N=yellow] {D}; \node[N=green, join] {E}; % <--- } \end{tblr} \end{document}

In both case you get the same result:

enter image description here

Addendum: Using @Qrrbrbirlbel answer on my [question2 the possible solution using graphs library can be:

\documentclass{article}

\usepackage{tabularray} \usepackage{tikz} \usetikzlibrary{arrows.meta, graphs}

\begin{document} \begin{table} \centering \tikzset{ N/.style = {fill=#1!20, rounded corners=2pt, alias=N, inner ysep=4mm, inner xsep=0mm, outer sep=0pt, minimum width=18mm}, N/.default = blue, } \tikzgraphsset{ branch right sep=5mm, every graph/.append style = {path, ->, /tikz/>=Straight Barb}}

\begin{tblr}{colspec = {Q[r] Q[r,wd=64mm]}, colsep = 1mm, rowsep = 0pt} zz: & \SetCell{l} something \ yy: & \tikz[baseline=(N.base)] \graph{A[N], B[N], C[N]}; \ xx: & \tikz[baseline=(N.base)] \graph{D[N=yellow], E[N=green]}; \end{tblr} \end{table} \end{document}

Compilation result is almost the same as before.

enter image description here

Zarko
  • 296,517
3

What about this? I used a normal tabular environment, put the tikzpictures in math mode and \vcentered them. Result

\documentclass{article}
\usepackage{tikz}

\begin{document} \begin{tabular}{rl} zz: & something\ yy: & $\vcenter{\hbox{\begin{tikzpicture}[baseline=(current bounding box.center),cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}] \path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C}; \draw[->] (A)--(B); \draw[->] (B)--(C); \end{tikzpicture}}}$ \ xx: & $\vcenter{\hbox{\begin{tikzpicture}[cc/.style={fill=blue!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cyellow/.style={align=center,fill=yellow!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},cgreen/.style={align=center,fill=green!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm},corange/.style={align=center,fill=orange!20,rounded corners=2pt,thick,inner sep=4,outer sep=0,minimum width=18mm}] \path (2.3,0) node[cyellow] (AA) {D} (4.6,0) node[cgreen] (BB) {E}; \draw[->] (AA)--(BB); \end{tikzpicture}}}$ \end{tabular} \end{document}

Οὖτις
  • 2,897
  • 1
  • 5
  • 18
  • thanks, it is almost right, but the D and E need to be under the B and the C... I put the coordinates correctly, but it seems like tikz pushes it away. I suppose I could put a hidden node somehow at (0,0) and that should fix it. – tikzuser Mar 03 '23 at 10:29
  • Sorry, I misread this part. I'd just insert a \hspace{23mm} (box width + arrow length) before \vcenter which gives the correct output, but this is quite improper and will not work as soon as your box becomes wider than its minimum width. – Οὖτις Mar 03 '23 at 10:48
  • thanks! my trick with a hidden (0,0) node worked! – tikzuser Mar 05 '23 at 06:45
3

It's probably better if you split tikzpicture into separate rows. Then you will have more flexibility to manipulate rows. I added baseline with slight shift to centre rows.

The reason you have such large space is because you define columns as X-type, which causes environment to create columns of the equal size. Because tikz creates a wide image, the left column mirrors that width, hence you get huge space. Simply change columns to Q[].

For the last issue, set alignment to right with exception to the first row.

I also slightly changed your code. I added a global style ccgeneric and then inherited the other styles based on that. This style takes a parameter: colour. It makes the code more readable, less cluttered and easier to manipulate if you need to make changes.

EDIT. The code corrected to add missing arrow tips.

\documentclass{article}

\usepackage{tabularray} \usepackage{tikz}

\tikzset{ baseline={([yshift=-2pt]current bounding box)}, every edge/.style={draw, ->}, ccgeneral/.style={ fill=#1, rounded corners=2pt, thick, inner sep=4, outer sep=0, minimum width=18mm, }, cc/.style={ccgeneral=blue!20}, cyellow/.style={ccgeneral=yellow!20}, cgreen/.style={ccgeneral=green!20}, corange/.style={ccgeneral=orange!20}, }

\begin{document} \begin{tblr}{ colspec = {Q[l,m]Q[r,m]}, rowsep = 0pt, colsep = 3pt, row{1} = {l}, } zz: & something \ yy: & \begin{tikzpicture} \path (0,0) node[cc] (A) {A} (2.3,0) node[cc] (B) {B} (4.6,0) node[cc] (C) {C}; \draw (A) edge (B) (B) edge (C); \end{tikzpicture} \ xx: & \begin{tikzpicture} \path (2.3,0) node[cyellow] (D) {D} (4.6,0) node[cgreen] (E) {E}; \draw (D) edge (E); \end{tikzpicture} \end{tblr} \end{document}

enter image description here

Celdor
  • 9,058
3

Here is a solution with {NiceTabular} of nicematrix (and Tikz for the arrows).

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\NewDocumentCommand{\Fill}{m}{\Block[fill=#1,rounded-corners]{}}

\begin{NiceTabular}{rw{c}{2cm}cw{c}{2cm}cw{c}{2cm}}[cell-space-limits=3mm] xx: & something \ yy: & \Fill{blue!15} A && \Fill{blue!15} B && \Fill{blue!15} C \ xx: & && \Fill{yellow!15} D && \Fill{green!15} E \ \CodeAfter \begin{tikzpicture} [->] \draw (2.5-|3) -- (2.5-|4) ; \draw (2.5-|5) -- (2.5-|6) ; \draw (3.5-|5) -- (3.5-|6) ; \end{tikzpicture} \end{NiceTabular}

\end{document}

As usual with nicematrix you need several compilations.

Output of the above code

F. Pantigny
  • 40,250