1

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?

3 Answers3

3

You can save some typing by using an array since the table contains mainly math.

\documentclass[a4paper,12pt]{article}
\usepackage{ragged2e,array}
\usepackage{xfp,booktabs}

\begin{document}
\Large
\renewcommand{\arraystretch}{1.2}
\setlength{\arraycolsep}{10pt}
\newcommand*\mc[3]{\multicolumn{#1}{#2}{\textbf{#3}}}
\[
   \begin{array}{|r|l|l|} \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{array}
\]

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
2

It's quite easy, but the result is really ugly. The trick is to define = as an alignment point.

However, you can find some ideas for simplifying your input (look at \bigstrut).

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage{array}

\newcommand{\bigstrut}{%
  \vrule height 1.2\ht\strutbox depth 1.2\dp\strutbox width 0pt
}

\begin{document}

\begin{center}

\newcommand{\equals}{=}
\catcode`==4

\Large
 \begin{tabular}{
   |>{\bigstrut}r
   |>{$}r<{\equals{}$}@{}>{$}l<{$}
   |>{$}r<{\equals{}$}@{}>{$}l<{$}|
 }
  \hline \multicolumn{3}{|c|}{\bfseries Opgave} & \multicolumn{2}{c|}{\bfseries 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}

enter image description here

How would I typeset it? Left aligned, with no vertical rule and just a few horizontal ones.

\documentclass[
  a4paper,
  12pt
]{article}

\usepackage{array,booktabs}

\begin{document}

\begin{center}

\Large
\begin{tabular}{@{} r >{$}l<{$} @{\qquad} >{$}l<{$} @{}}
\toprule
& \multicolumn{1}{l}{\bfseries Opgave} & \multicolumn{1}{@{}l}{\bfseries Facit} \\
\midrule
  1. & 3x - 7       = 2            & x = 3    \\
\addlinespace
  2. & 10           = 7 + x \div 4 & x = 12   \\
\addlinespace
  3. & 2x - 5       = 9            & x = 7    \\
\addlinespace
  4. & 2x + 5       = 3x + 9       & x = -4   \\
\addlinespace
  5. & 7x - 22      = x + 8        & x = 5    \\
\addlinespace
  6. & 24x - 16     = 32           & x = 2    \\
\addlinespace
  7. & 3x + 18      = 12           & x = -2   \\
\addlinespace
  8. & 18           = 2x + 8       & x = 5    \\
\addlinespace
  9. & 3x + 100     = 250          & x = 50   \\
\addlinespace
 10. & 21           = 4x + 9       & x = 3    \\
\addlinespace
 11. & 4x - 10 + 2x = 4x           & x = 5    \\
\addlinespace
 12. & 2x - 5       = 9            & x = 7    \\
\bottomrule
\end{tabular}
\end{center}

\end{document}

enter image description here

egreg
  • 1,121,712
1

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}
Werner
  • 603,163