3

I have a table with three columns; one is a text column, one contains images, and the last one a tikz graphics. But I am not being able to vertically align them. Somewhat minimal code:

\documentclass[demo]{beamer}

\usepackage{geometry} \geometry{paperwidth=7.5in,paperheight=11in}

\usepackage{tabularray} \usepackage[export]{adjustbox} \usepackage{xcolor} \usepackage{tikz}

\newcommand{\red}[1]{\textcolor{red}{#1}} \newcommand{\animal}{\includegraphics[width=0.1\linewidth, height=0.1\linewidth]{animal.png}\hspace{1em}}

\newcommand{\mybox}{\tikz{\path[draw, line width=2pt] (0,0) rectangle (0.6in, 0.6in)}}

\begin{document} \section{Introduction} \begin{frame}{} \begin{tblr}{ colspec={X[0.1]XX[0.12]@{}}, columns = {m}, column{1} = {font=\Huge}, hlines = {}{2-3}{}, } \red{B} & \animal \animal \hspace{3em} \animal \animal \animal \animal \animal & \mybox\ \end{tblr} \end{frame} \end{document}

The output:

enter image description here

I have tried using the valign=c option from the adjustbox package. But that didn't also work.

Modification for valign:

\newcommand{\animal}{\includegraphics[width=0.1\linewidth, height=0.1\linewidth, valign=c]{animal.png}\hspace{1em}}

Adjustbox output:

enter image description here

How may I solve this?

Edit: My question presentation has created some confusion. So, I am updating the code a bit. Now I have added the all the horizontal and vertical lines to make it easier to show my problem.

\documentclass[demo]{beamer}

\usepackage{geometry} \geometry{paperwidth=7.5in,paperheight=11in}

\usepackage{tabularray} \usepackage[export]{adjustbox} \usepackage{xcolor} \usepackage{tikz}

\newcommand{\red}[1]{\textcolor{red}{#1}} \newcommand{\animal}{\includegraphics[width=0.1\linewidth, height=0.1\linewidth]{animal.png}\hspace{1em}}

\newcommand{\mybox}{\tikz{\path[draw, line width=2pt] (0,0) rectangle (0.6in, 0.6in)}}

\begin{document} \section{Introduction} \begin{frame}{} \begin{tblr}{ colspec={X[0.1, c]XX[0.12]@{}}, columns = {m}, column{1} = {font=\Huge}, hlines = {}{1-3}{}, vlines } \red{B} & \animal \animal \hspace{3em} \animal \animal \animal \animal \animal & \mybox\ \end{tblr} \end{frame} \end{document}

Output:

enter image description here

My expected output:

enter image description here

Imran
  • 3,096
  • 1
    How do you want them aligned exactly? What you need to do is think about the baseline of each. You can specify the baseline of the TikZ picture using the baseline option of the tikzpicture environment. The tikzpicture looks taller than the others, so you how should that difference be accommodated? – cfr Oct 08 '23 at 16:42
  • 1
    For the image specifying one prameter, e.g. height is sufficient ... perhaps that solves your problem already? Images are rescaled to meet said height. – MS-SPO Oct 08 '23 at 17:27
  • @cfr Thanks for the direction. I have been able to vertically center the tikz image by tweaking the baseline key of the tikz command (haven't yet been able to solve for the 1st column). And to answer your query, I want to vertically align all columns in the center. – Imran Oct 08 '23 at 17:34
  • @MS-SPO In my original code, I have specified only the width key. In the MWE, I added the height key (along with width) to visually resemble my original output. Here is an output of the MWE if I don't specify the height key. – Imran Oct 08 '23 at 17:38
  • @Imran: ok. However, the code you posted uses both parameters. – MS-SPO Oct 08 '23 at 17:41
  • What's wrong with the alignment of the first? They look aligned in the image you posted. – cfr Oct 08 '23 at 17:47
  • @cfr The first column isn't vertically centered. The text in the 1st column is aligned to the bottom of the row. – Imran Oct 08 '23 at 17:50
  • In the images you posted, the first column has the same height as the second, doesn't it? And only the third column looks misaligned. – cfr Oct 08 '23 at 20:56
  • 1
    @cfr I am confused if I am using the word "align" properly. So, I have attached an image of the expected output. Hope that clears any confusion. – Imran Oct 09 '23 at 05:48
  • You are using 'align' fine. It just didn't occur to be 'B' was part of the row. I saw it as outside the tabular because the grey lines above and below said 'tabular here!' ;). – cfr Oct 09 '23 at 23:50

3 Answers3

4

valign=M and \tikz[baseline=(current bounding box.center)].

\newcommand{\animal}{\includegraphics[width=0.1\linewidth, height=0.1\linewidth, valign=M]{animal.png}\hspace{1em}}
\newcommand{\mybox}{\tikz[baseline=(current bounding box.center)]{\path[draw, line width=2pt] (0,0) rectangle (0.6in, 0.6in)}}

enter image description here

Clara
  • 6,012
  • Thank you for the help. Using this code, however, doesn't align the vertically align the text in the 1st column. The output: https://imgur.com/XMc1ClA ; Desired output: https://imgur.com/AWMcA3q – Imran Oct 09 '23 at 06:01
  • @Imran You can try \tikz[baseline=0.235in]. – Clara Oct 09 '23 at 10:01
3

This is an answer ... but I don't think you're going to like it.

aligned boxes and things

The basic problem is that TeX aligns things to the baseline, but tabulararry isn't very clear about what exactly the various options and specifications do. (Or perhaps it is just a cross-referencing problem and the details are not with the explanations I found.) This means that what matters about some examples is left to the reader to determine. For example, it might not occur to somebody that it really matters whether the example contains multi-line text rather than, say, an image.

The rather substantive complication is the use of \Huge for a single column. This makes it harder to adjust things. In the code below, I've done this by measuring the height of \Huge x to get the 'x' height, half of which is what we want to adjust by. Note that if the first column contained x rather than B, we'd want half of half the 'x' height.

\documentclass[demo]{beamer}

\usepackage{geometry} \geometry{paperwidth=7.5in,paperheight=11in}

\usepackage{tabularray} \usepackage{calc} \usepackage{tikz}

\newcommand{\red}[1]{\textcolor{red}{#1}} \newlength\mylength \newlength\mylengtha \newlength\mylengthb \newcommand{\animal}{\raisebox{.5\mylengthb}{\begin{minipage}[c]{\mylength}\includegraphics[width=\mylengtha, height=\mylengtha]{animal.png}\end{minipage}\hspace{.05em}}}

\newcommand{\mybox}{\raisebox{.5\mylengthb}{\begin{minipage}[c]{\mylength}\tikz{\path[draw, line width=2pt] (0,0) rectangle (0.6in, 0.6in);}\end{minipage}}}

\begin{document} \setlength\mylength{.075\linewidth+1em} \setlength\mylengtha{.075\linewidth} \settoheight\mylengthb{\Huge x} \section{Introduction} \begin{frame} \begin{tblr}{ colspec={X[0.1, c]XX[0.18,c]@{}}, columns = {m}, column{1} = {font=\Huge}, hlines = {}{1-3}{}, vlines } \red{B} & \animal \animal \hspace{2.5em} \animal \animal \animal \animal \animal & \mybox\ \end{tblr} \end{frame} \end{document}

The effect of putting things in minipages is to introduce some padding, so I've reduced various dimensions for the example.

If all this seems like a lot of work, I recommend xcoffins. The package is experimental, but the underlying programming layer isn't (so if the package disappears in a puff of smoke or gets turned into a frog or something, you could recreate the commands fairly easily - or somebody on this site could do so if you don't like expl3).

Off-topic but the use of beamer here seems somewhat egregious. Why are you using this class with geometry?

cfr
  • 198,882
  • 1
    Thank you for the solution. It worked perfectly. And about your question: the person I am working for wanted it this way (beamer class with this page size), so I used the geometry package (PS now I have found out that I didn't need to explicitly load the package). – Imran Oct 10 '23 at 14:48
  • 1
    @Imran Fair enough. Yes, I think beamer uses geometry. This just seemed an odd size for slides. There is also article mode, of course, but that's rather different. And beamerposter. But if that's what they want, that's what they want, I guess. – cfr Oct 10 '23 at 19:34
2

With

  • tikzwith [baseline] in the first column
  • valign=M (the Clara's answer)
  • tikzwith baseline=(current bounding box.center) in the 3rd column

The code

\documentclass{article}
\usepackage{geometry}
\geometry{paperwidth=7.5in,paperheight=11in}
\usepackage{showframe}%<-- comment 
\usepackage{tabularray}
\usepackage[export]{adjustbox}
\usepackage{tikz}

\tikzset{ letter/.style={ %draw,
red, inner sep=0pt, outer sep=0pt } } \newcommand{\red}[1]{\tikz [baseline]\node[letter] {#1};}%

\newcommand{\animal}{% \includegraphics[width=0.1\linewidth, height=0.1\linewidth,valign=M]{example-image-duck}\hspace{1em}% }

\newcommand{\mybox}{\tikz[baseline=(current bounding box.center)]{\path[draw, line width=2pt] (0,0) rectangle (0.6in, 0.6in)}}

\begin{document} \noindent%<-- overfull 15pt \begin{tblr}{ %colspec={X[0.1, c]XX[0.12]@{}},%<-- overful with 0.12 colspec={X[0.1,c]X[c]X[0.3,c]},% columns = {m}, column{1} = {font=\Huge}, hlines = {}{1-3}{}, vlines, } \red{B}&\fbox{\animal \animal \hspace{3em} \animal \animal} & \mybox\ \end{tblr} \end{document}

enter image description here

pascal974
  • 4,652