lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
253 stars 22 forks source link

Tikzpicture not centering correctly #494

Closed lastras closed 5 months ago

lastras commented 5 months ago

I am having difficulties centering a tikzpicture vertically. Here's an example where it doesn't seem to work, yet when I add a ```\\''' it works (except that now I added space I didn't want).

Environment: Overleaf, pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=pdflatex 2022.8.9)

\documentclass{article}
\usepackage{tikz}
\usepackage{tabularray}

\begin{document}

First version:
\begin{tblr}{colspec={|Q[c]|Q[c]|Q[c]|},rowspec={|Q[m]|Q[m]|Q[m]|}}
 {Alpha \\ Alpha} & Beta& Gamma \\
 Delta            & { \begin{tikzpicture} \draw (0,0)[color=black,fill=white] rectangle (3,3); \end{tikzpicture} } & {Zeta \\ Zeta}  \\
 Eta              & {Theta \\ Theta}&  abc   \\
\end{tblr}
\\
\vspace{1in} \\
Second version:
\begin{tblr}{colspec={|Q[c]|Q[c]|Q[c]|},rowspec={|Q[m]|Q[m]|Q[m]|}}
 {Alpha \\ Alpha} & Beta& Gamma \\
 Delta            & {  \\ \begin{tikzpicture} \draw (0,0)[color=black,fill=white] rectangle (3,3); \end{tikzpicture} } & {Zeta \\ Zeta}  \\
 Eta              & {Theta \\ Theta}&  abc   \\
\end{tblr}

\end{document}
Screenshot 2024-04-04 at 5 12 42 PM
muzimuzhi commented 5 months ago

Try either \begin{tikzpicture}[baseline=(current bounding box.center)] or my experimental implementation of valign=M in https://github.com/lvjr/tabularray/issues/434#issuecomment-1686957209.

By default a tikzpicture has its baseline at the height of y = 0pt, see https://tikz.dev/tikz-scopes#pgf./tikz/baseline and https://github.com/lvjr/tabularray/discussions/481.

lastras commented 5 months ago

\begin{tikzpicture}[baseline=(current bounding box.center)] worked. Thank you!!!

muzimuzhi commented 5 months ago

Depending on the content of tikzpicture, you may also need some negative y-shifting, to make tikzpicture and text cells centered vertically and visually.

\documentclass{article}
\usepackage{tikz}
\usepackage{tabularray}

\begin{document}

\newcommand{\tikzcell}{%
  \begin{tikzpicture}
    \draw (0,0) rectangle (3,3);
    \draw (0,0) -- (3,3) (0,3) -- (3,0) (0,1.5) -- (3,1.5);
  \end{tikzpicture}%
}

\begin{tblr}{colspec={|Q[c]|Q[c]|Q[c]|},rowspec={|Q[m]|Q[m]|Q[m]|}}
  {Alpha \\ Alpha} & Beta             & Gamma          \\
  Delta            & \tikzcell        & {Zeta \\ Zeta} \\
  Eta              & {Theta \\ Theta} &  abc           \\
\end{tblr}
Before

\begin{tblr}{
  colspec={|Q[c]|Q[c]|Q[c]|},
  rowspec={|Q[m]|Q[m]|Q[m]|},
  cell{2}{2}={preto=
    \tikzset{every picture/.style=
      {baseline=(current bounding box.center)}}}
}
  {Alpha \\ Alpha} & Beta             & Gamma          \\
  Delta            & \tikzcell        & {Zeta \\ Zeta} \\
  Eta              & {Theta \\ Theta} &  abc           \\
\end{tblr}
Setting \verb|\begin{tikzpicture}[basline={(...)}]|

\begin{tblr}{
  colspec={|Q[c]|Q[c]|Q[c]|},
  rowspec={|Q[m]|Q[m]|Q[m]|},
  cell{2}{2}={preto=
    \tikzset{every picture/.style=
      {baseline={([yshift=-.75ex]current bounding box.center)}}}}
}
  {Alpha \\ Alpha} & Beta             & Gamma          \\
  Delta            & \tikzcell        & {Zeta \\ Zeta} \\
  Eta              & {Theta \\ Theta} &  abc           \\
\end{tblr}
Setting \verb|\begin{tikzpicture}[basline={([yshift=]...)}]|

\end{document}
image