在表格列中对齐 =

在表格列中对齐 =

请考虑以下示例:

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

输出

我如何自动对齐第二列和第三列的表达式=

答案1

以下示例使用列特定的对齐方式,设置埃夫特Hside 和rHs收件箱的一侧=。第一次需要进行两次编译:

在此处输入图片描述

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

答案2

array由于该表主要包含数学知识,因此您可以使用来节省一些输入内容。

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

在此处输入图片描述

答案3

这很简单,但结果确实很难看。诀窍是将其定义=为对齐点。

但是,您可以找到一些简化输入的想法(查看\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}

在此处输入图片描述

我该如何排版?左对齐,没有垂直线,只有几条水平线。

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

在此处输入图片描述

相关内容