2

I would like to put multiple equations in a table like below.

solved for x solved for y
x = a + b (1.1) y = d + e (1.2)
x = d + b * k (1.3) y= j^2 (1.4)

the equation should be aligned to the left and the numbering to the right. I've not been able to fit the equation envrioment into a table so the equations will be numbered.

3
  • 1
    Welcome! Is it possible that this question is a duplicate of tex.stackexchange.com/questions/78579/labelled-in-line-equation (with Heiko's answer )?
    – Rmano
    Commented Nov 15, 2022 at 15:40
  • Not really. If i use the inline equation, the numeration is not oriented to the right of the cell and if I have a fraction it is made smaller in the inlineequation compared to the equation envrioment.
    – Olerf
    Commented Nov 15, 2022 at 15:52
  • 1
    @Olerf If you are using columns with defined width, the alignment can easily be done using \hfill
    – cabohah
    Commented Nov 15, 2022 at 16:14

2 Answers 2

6

Update
As pointed out in the comments, the first version did not add an option to refer to equations. Hence, \addtag was redesigned and moved directly inside the cells making numbering optional. Additionally, an argument of \addtag[<label>] is optional. It is a label name and is required only if the equation needs to be referenced.

enter image description here

The new code

\documentclass{article}
\usepackage{array}
\usepackage{etoolbox}
\newcommand\addtag[1][]{%
  \refstepcounter{equation}\hfill(\theequation)%
  \notblank{#1}{\label{#1}}{}}
\counterwithin[\arabic]{equation}{section}


\begin{document}
\section{First section}
\begin{table}[tbh]
  \renewcommand*{\arraystretch}{1.5}
  \centering
  \caption{Equations}\label{tab:equations}
  \begin{tabular}{| *2{>{\(}p{5cm}<{\)}|}}
    \hline
    \textbf{Solved for $x$} & \textbf{Solved for $y$} \\
    \hline
    x = a + b          \addtag[a] & y = d + e \addtag[b] \\ 
    x = d + b \times k            & y = j^2   \addtag[c] \\ 
    \hline
  \end{tabular}
\end{table}

References: Euqation~\ref{a}, Equation~\ref{b}, and Equation~\ref{c}.
\end{document}

EDIT.
Note the equation numbers. The document class article defines equations as single numbers, whereas report and book will add a chapter to form a combined tag, that is (2.1) etc. as long as \chapter is used. If you wish to have the same effect using just article, add the following code to preamble:

 \counterwithin[\arabic]{equation}{section}

EDIT2. (display style)
Inline equations by default are typeset using a text style, which is a more compact form to fit expressions between texts. This can be changed by appending \displaystyle to each expression. However, >{...} can do that automatically per each cell in the column. See the snippet below. You just need to add \displaystyle after \(

  \begin{tabular}{| *2{>{\(\displaystyle}p{5cm}<{\)}|}}
    % ...
  \end{tabular}

Equations may no longer fit the rows after the change. In that case, increase the stretching factor of \arraystretch to something larger than 1.8, e.g.

\renewcommand*{\arraystretch}{1.85}

Here's a simple solution

enter image description here

\documentclass[12pt]{article}
\usepackage{array}
\newcommand\addtag{\;\refstepcounter{equation}(\theequation)}
\begin{document}
\begin{table}[tbh]
  \renewcommand*{\arraystretch}{1.5}
  \centering
  \caption{Equations}\label{tab:equations}
  \begin{tabular}{| *2{>{\(}p{5cm}<{\hfill\addtag\)}|}}
    \hline
    \multicolumn{1}{|l|}{\textbf{Solved for $x$}}
    & \multicolumn{1}{l|}{\textbf{Solved for $y$}} \\
    \hline
    x = a + b          & y = d + e \\
    x = d + b \times k & y = j^2 \\
    \hline
  \end{tabular}
\end{table}
\end{document}
4
  • Adding the tag via <{…} code is a nice idea, but it makes it difficult to set labels to the equations if needed.
    – cabohah
    Commented Nov 15, 2022 at 16:23
  • Thanks for pointing this out. I've addressed the issue.
    – Celdor
    Commented Nov 15, 2022 at 17:21
  • @Celdor I've used your version, thanks! the only thing i have a problem with are fracs inside the table. They are displayed way smaller than inside the equation envrioment. Is there any easy fix to that problem?
    – Olerf
    Commented Nov 16, 2022 at 19:16
  • 1
    @Olerf That's an easy change. See Edit2 in my answer. In general, inline equations, in texts, are rendered using \textstyle as they would take too much space, otherwise. If you add \displaystyle in front of an expression, you tell LaTeX to use the regular format.
    – Celdor
    Commented Nov 16, 2022 at 21:17
5

As @Rmano already suggested in a comment, this can be done using Heiko's answer here. To do the right alignment in a column, you just have to use columns with defined width and \hfill instead of just a non-breakable space ~. Here a suggestion with a slightly improved code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\makeatletter
\NewDocumentCommand{\InlineEquation}{om}{%
  \begingroup
    % Put \refstepcounter at the beginning, because
    % package `hyperref' sets the anchor here.
    \refstepcounter{equation}%
    % prevent line breaks inside equation
    \relpenalty=10000 %
    \binoppenalty=10000 %
    \ensuremath{%
      % \displaystyle % larger fractions, ...
      \IfNoValueF{#1}{\label{#1}}% add label if optional argument is used
      #2%
    }%
    \hfill\@eqnnum
  \endgroup
}
\makeatother

\begin{document}
\noindent\begin{tabularx}{\textwidth}{|X|X|}
  \hline
  \textbf{solved for $x$} & \textbf{solved for $y$}\\
  \hline
  \InlineEquation[eq:first]{x = a + b} & \InlineEquation[eq:second]{y =d + e}\\
  \InlineEquation[eq:third]{x = d + b \times k} & \InlineEquation{y = j^{2}} \\
  \hline
\end{tabularx}

See \eqref{eq:first}, \eqref{eq:second}, \eqref{eq:third} and the one without
label.
\end{document}

table with inline math numbered

BTW: I would recommend to not use vertical lines and lots of horizontal lines. See package booktabs for information about how to make nice tables.

If the equation number should also depend on the table number, you could use:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{caption}% To have captions formatting to be used above table instead of below.
\newcounter{outsideequation}
\makeatletter
\NewDocumentCommand{\TableEquation}{om}{%
  \begingroup
    % Put \refstepcounter at the beginning, because
    % package `hyperref' sets the anchor here.
    \refstepcounter{equation}%
    % prevent line breaks inside equation
    \relpenalty=10000 %
    \binoppenalty=10000 %
    \ensuremath{%
      % \displaystyle % larger fractions, ...
      \IfNoValueF{#1}{\label{#1}}% add label if optional argument is used
      #2%
    }%
    \hfill\@eqnnum
    \endgroup
}
\newenvironment{UseTableEquations}{%
  \setcounter{outsideequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \counterwithin{equation}{table}%
}{%
  \setcounter{equation}{\value{outsideequation}}%
  \counterwithout{equation}{table}%
}
\makeatother
\begin{document}
\begin{table}
  \renewcommand*{\arraystretch}{1.2}
  \caption{Table with equations}
  \begin{UseTableEquations}
    \begin{tabularx}{\textwidth}{XX}
      \toprule
      \textbf{solved for $x$} & \textbf{solved for $y$}\\
      \midrule
      \TableEquation[eq:first]{x = a + b} & \TableEquation[eq:second]{y =d +
        e}\\
      \addlinespace
      \TableEquation[eq:third]{x = d + b \times k} & \TableEquation{y = j^{2}} \\
      \bottomrule
    \end{tabularx}
    \end{UseTableEquations}
\end{table}

See \eqref{eq:first}, \eqref{eq:second}, \eqref{eq:third} and the one without
label.

\end{document}

equation number depending on table number

or with table caption below the table:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\newcounter{outsideequation}
\makeatletter
\NewDocumentCommand{\TableEquation}{om}{%
  \begingroup
    \advance\c@table by 1\relax% because table counter is 1 to low
    % before \caption
    % Put \refstepcounter at the beginning, because
    % package `hyperref' sets the anchor here.
    \refstepcounter{equation}%
    % prevent line breaks inside equation
    \relpenalty=10000 %
    \binoppenalty=10000 %
    \ensuremath{%
      % \displaystyle % larger fractions, ...
      \IfNoValueF{#1}{\label{#1}}% add label if optional argument is used
      #2%
    }%
    \hfill\@eqnnum
    \endgroup
}
\newenvironment{UseTableEquations}{%
  \setcounter{outsideequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \counterwithin{equation}{table}%
}{%
  \setcounter{equation}{\value{outsideequation}}%
  \counterwithout{equation}{table}%
}
\makeatother
\begin{document}
\begin{table}
  \renewcommand*{\arraystretch}{1.2}
  \begin{UseTableEquations}
    \begin{tabularx}{\textwidth}{XX}
      \toprule
      \textbf{solved for $x$} & \textbf{solved for $y$}\\
      \midrule
      \TableEquation[eq:first]{x = a + b} & \TableEquation[eq:second]{y =d +
        e}\\
      \addlinespace
      \TableEquation[eq:third]{x = d + b \times k} & \TableEquation{y = j^{2}} \\
      \bottomrule
    \end{tabularx}
    \end{UseTableEquations}
  \caption{Table with equations}
\end{table}

See \eqref{eq:first}, \eqref{eq:second}, \eqref{eq:third} and the one without
label.

\end{document}

caption below table

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .