math-mode,tabularray – Fixing Compilation Issues in Tabularray with Math Environment

math-modetabularray

I have this code

\documentclass{report}
\usepackage{tabularray}
\usepackage{mathtools}
\NewColumnType{C}{>{$}c<{$}}
\def\myformula{Area = side \times side \\ Perimeter = side \times 4}
\begin{document}
    \begin{tblr}{
            colspec={cX},
            row{2}={C,fg=white,bg=black}
        }
        {One} & {Two} & \SetCell[c=2]{C}{\myformula} \\
    \end{tblr}
\end{document}

This code doesn't compile, the most relevant error during compilation ( reported on row 12 ) is:

Missing $ inserted. \end

I understand that using a math environment inside a tabular one is more complicated than usual, for this reason I tried to use \NewColumnTypeto make my life easier but apparently I'm missing something and I already tried a lot of variations.

What I'm missing ? Something to do with how Latex3 works ?

Best Answer

  • Which version of \tabularray you use?
  • Your MWE (Minimal Working Example) contain many errors. From definition of myformula to number of defined columns ...
  • Syntax of tabularray package differ from syntax of "classic" packages for tables. For example, it define option for math mode of columns. For details see package documentation.
  • I guess, that you looking for the following result:

enter image description here

With recent version of \tabularray (2022A) your MWE should be:

\documentclass{report}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\def\myformula{\begin{gathered}
    \mathrm{Area} = \mathrm{side} \times \mathrm{side} \\ \mathrm{Perimeter} = \mathrm{side} \times 4
                \end{gathered}}

\begin{document}
\noindent%    
\begin{tblr}{colspec={cc X[c, m] X[c, m] },
                  row{1}={fg=white,bg=black}
                }
One &   Two & \SetCell[c=2]{c,$$}{\myformula} 
                    &       \\
\end{tblr}
\end{document}

Edit: I cases, when you like to have aligned equations at = sign, you need to replace gathered with aligned environment (both are defined in amsmath package):

\def\myformula{\begin{aligned}
    \mathrm{Area}   & = \mathrm{side} \times \mathrm{side}
\mathrm{Perimeter}  & = \mathrm{side} \times 4
               \end{aligned}}

using above in suggested MWE gives the following result.

enter image description here

If you still have some open problems, I suggest you to ask new question where clearly (maybe with link to this) where clearly explain what is your problem.

Related Question