Search code examples
latextabularverbatim

LuaLaTeX: horitontal alignment of verbatim-environments inside a tabularx-environment


First, I'm fully aware that my code is probably not the ideal way to realize what I want to do. However I'm not a professional LaTeX User and this is the way I figured out.

I made a minimal example that hopefully works when compiled(compiler: LuaLaTeX) to display my problem. I'm trying to make a beamer-frame with a tabularx-table, which contains in-line verbatim-environments on the one hand and equation-environments on the other. The "Y" column-type is a modified form of the "X" environment of tabularx, which I found on another Stackoverflow-thread.

The precise problem now is the following: I'd like the verbatim-expressions to be aligned with the equation-expressions or at least to be vertically centered in each cell.

As mentioned, I'm far from being an expert and I've exhausted all of my Ideas, so I'm very thankful for any form of ideas and suggestions. :)

\documentclass[c, 10pt]{beamer}

\usepackage{polyglossia}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{siunitx}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{siunitx}
\usepackage{placeins}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{nicefrac}
\usepackage{array}

\setdefaultlanguage{english}

\usetheme{JuanLesPins}
\usecolortheme{seahorse}

\newcolumntype{Y}{>{\centering\arraybackslash} X}

\begin{document}

\begin{frame}[fragile, allowframebreaks]{Symbols and Commands}
    \begin{block}{\centering \large{Division}}
        \begin{table}[h]
            \centering
            \renewcommand\baselinestretch{0.01}\selectfont
            \begin{tabularx}{\textwidth}{Y Y Y}
                \toprule
                \multicolumn{1}{m{.3\textwidth}}{\centering Code} & \multicolumn{2}{m{.6\textwidth}}{\centering Examples}\\
                \midrule 
                \verb|\dfrac{a}{b}| & 
                {\begin{equation*}
                    \dfrac{a}{b}
                    \end{equation*}} & 
                {\begin{equation*}
                    \mathrm{e}^{\dfrac{1}{k_BT}}
                    \end{equation*}} \\
                \verb|\frac{a}{b}| &
                {\begin{equation*}
                    \frac{a}{b}
                    \end{equation*}} &
                {\begin{equation*}
                    \mathrm{e}^{\frac{1}{k_BT}}
                    \end{equation*}} \\
                \verb|\nicefrac{a}{b}| &
                {\begin{equation*}
                    \nicefrac{a}{b}
                    \end{equation*}} &
                {\begin{equation*}
                    \mathrm{e}^{\nicefrac{1}{k_BT}}
                    \end{equation*}} \\
                \bottomrule
            \end{tabularx}
        \end{table}
    \end{block}
\end{frame}    

\end{document}

Solution

  • To place unnumbered equations in a table, I would rather use inline math instead of equation environments. To get the same rendering of fractions as in equations, add \displaystyle.

    Off-topic:

    • don't load packages multiple times

    • don't use fragile, allowframebreaks unless they are really necessary for the frame

    • floating specifier like [H] don't make sense in a document class without floating mechanism

    • you don't need \centering in beamer tables, they are centred by default

    • the syntax \large{...} is wrong, \large is a switch and does not take an argument, so it should be \large Division. Anyway it would be better to not put formatting instructions inside an macro argument, but to set the appropriate beamer template that controls the font of the block title


    \documentclass[c, 10pt]{beamer}
    
    %\usepackage{polyglossia}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{siunitx}
    \usepackage{tabularx}
    %\usepackage{amsmath}
    %\usepackage{amssymb}
    %\usepackage{siunitx}
    %\usepackage{placeins}
    \usepackage{multirow}
    \usepackage{booktabs}
    %\usepackage{verbatim}
    %\usepackage{fancyvrb}
    \usepackage{nicefrac}
    \usepackage{array}
    
    
    %\setdefaultlanguage{english}
    
    \usetheme{JuanLesPins}
    \usecolortheme{seahorse}
    
    \newcolumntype{Y}{>{\centering\arraybackslash} X}
    
    \setbeamerfont{block title}{size=\large}
    
    \begin{document}
    
    \begin{frame}
        \frametitle{Symbols and Commands}
        \begin{block}{\centering Division}
            \begin{table}
    %            \centering
    %            \renewcommand\baselinestretch{0.01}\selectfont
                \begin{tabularx}{\textwidth}{Y Y Y}
                    \toprule
                     Code & \multicolumn{2}{c}{Examples}\\
                    \midrule 
                    \verb|\dfrac{a}{b}| & 
                    $\displaystyle\dfrac{a}{b}$ & 
                    $\displaystyle\mathrm{e}^{\dfrac{1}{k_BT}}$ \\\addlinespace
                    \verb|\frac{a}{b}| &
                    $\displaystyle\frac{a}{b}$ &
                    $\displaystyle\mathrm{e}^{\frac{1}{k_BT}}$ \\\addlinespace
                    \verb|\nicefrac{a}{b}| &
                    $\displaystyle\nicefrac{a}{b}$ &
                    $\displaystyle\mathrm{e}^{\nicefrac{1}{k_BT}}$ \\
                    \bottomrule
                \end{tabularx}
            \end{table}
        \end{block}
    \end{frame}    
    
    \end{document}