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?

4
  • 1
    Have a look at this.
    – TeXnician
    Jun 15, 2017 at 20:04
  • \saenk doesn't seem necessary. Or rather, why do you use it?
    – Werner
    Jun 15, 2017 at 20:10
  • @TeXnician Thanks! The only problem is that now the text is pushed upwards in each cell, i.e., it's no longer vertically centered. (See comment to Werner.) Jun 15, 2017 at 20:15
  • @Werner Try to remove it from one the the cells and see what happens. :-) The text is then no longer vertically center inside the cell. Jun 15, 2017 at 20:16

3 Answers 3

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}
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

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

2
  • Thanks you very much! You say it looks ugly; how would you do it? Jun 15, 2017 at 20:21
  • 2
    @SvendTveskæg Left alignment, definitely. See edit.
    – egreg
    Jun 15, 2017 at 20:30

You must log in to answer this question.

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