[Tex/LaTex] Align at = in table column

horizontal alignmenttables

Consider the following example:

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage{ragged2e,array}
\usepackage{xfp}

\def\mlr{1.2}
\newcommand*\saenk[1]{\raisebox{\fpeval{-0.5*\mlr}ex}{#1}}
\newcommand*\mc[3]{\multicolumn{#1}{#2}{\saenk{\textbf{#3}}}}


\begin{document}

\begin{center}
\Large
 \begin{tabular}{
   |>{\RaggedLeft\arraybackslash}m{0.7cm}
   |>{\centering\arraybackslash}m{6cm}
   |>{\centering\arraybackslash}m{3cm}|
 }
  \hline \mc{2}{|c}{Opgave}                  & \mc{1}{|c|}{Facit} \\[\mlr ex]
  \hline  1. & $3x - 7       = 2$            & \saenk{$x = 3$}    \\[\mlr ex]
  \hline  2. & $10           = 7 + x \div 4$ & \saenk{$x = 12$}   \\[\mlr ex]
  \hline  3. & $2x - 5       = 9$            & \saenk{$x = 7$}    \\[\mlr ex]
  \hline  4. & $2x + 5       = 3x + 9$       & \saenk{$x = -4$}   \\[\mlr ex]
  \hline  5. & $7x - 22      = x + 8$        & \saenk{$x = 5$}    \\[\mlr ex]
  \hline  6. & $24x - 16     = 32$           & \saenk{$x = 2$}    \\[\mlr ex]
  \hline  7. & $3x + 18      = 12$           & \saenk{$x = -2$}   \\[\mlr ex]
  \hline  8. & $18           = 2x + 8$       & \saenk{$x = 5$}    \\[\mlr ex]
  \hline  9. & $3x + 100     = 250$          & \saenk{$x = 50$}   \\[\mlr ex]
  \hline 10. & $21           = 4x + 9$       & \saenk{$x = 3$}    \\[\mlr ex]
  \hline 11. & $4x - 10 + 2x = 4x$           & \saenk{$x = 5$}    \\[\mlr ex]
  \hline 12. & $2x - 5       = 9$            & \saenk{$x = 7$}    \\[\mlr ex]
  \hline
 \end{tabular}
\end{center}

\end{document}

output

How do I automatcailly align the expressions in both the second and third column at the =s?

Best Answer

The following example uses a column-specific alignment, setting the lefthand side and righthand side of the = in boxes. It requires two compilations on the first go:

enter image description here

\documentclass{article}

\usepackage{ragged2e,array,collcell,eqparbox}

\newcommand*\mc[3]{\multicolumn{#1}{#2}{\textbf{#3}}}

\makeatletter
\def\mycol{}
\newcommand{\processeq}[1]{%
  \@processeq#1\relax
  \eqmakebox[lhs\mycol][r]{$\lhs$}%
  ${}={}$%
  \eqmakebox[rhs\mycol][l]{$\rhs$}%
}
\def\@processeq$#1=#2$\relax{\def\lhs{#1}\def\rhs{#2}}
\makeatother

\begin{document}

\begin{center}
  \Large\renewcommand{\arraystretch}{1.2}%
  \begin{tabular}{
    |>{\RaggedLeft\arraybackslash}m{0.7cm}
    |>{\centering\arraybackslash\def\mycol{left}\collectcell\processeq}m{6cm}<{\endcollectcell}
    |>{\centering\arraybackslash\def\mycol{right}\collectcell\processeq}m{3cm}<{\endcollectcell}|
  }
    \hline \mc{2}{|c}{Opgave}                  & \mc{1}{|c|}{Facit} \\
    \hline  1. & $3x - 7       = 2$            & $x = 3$    \\
    \hline  2. & $10           = 7 + x \div 4$ & $x = 12$   \\
    \hline  3. & $2x - 5       = 9$            & $x = 7$    \\
    \hline  4. & $2x + 5       = 3x + 9$       & $x = -4$   \\
    \hline  5. & $7x - 22      = x + 8$        & $x = 5$    \\
    \hline  6. & $24x - 16     = 32$           & $x = 2$    \\
    \hline  7. & $3x + 18      = 12$           & $x = -2$   \\
    \hline  8. & $18           = 2x + 8$       & $x = 5$    \\
    \hline  9. & $3x + 100     = 250$          & $x = 50$   \\
    \hline 10. & $21           = 4x + 9$       & $x = 3$    \\
    \hline 11. & $4x - 10 + 2x = 4x$           & $x = 5$    \\
    \hline 12. & $2x - 5       = 9$            & $x = 7$    \\
    \hline
  \end{tabular}
\end{center}

\end{document}
Related Question