我想要将多个方程式放入如下表所示的表格中。
解为 x | 求解 y |
---|---|
x=a+b(1.1) | y=d+e(1.2) |
x = d + b * k (1.3) | y=j^2(1.4) |
方程式应左对齐,编号应右对齐。我无法将方程式环境放入表格中,因此方程式将被编号。
答案1
更新
正如评论中指出的那样,第一个版本没有添加引用方程的选项。因此,\addtag
被重新设计并直接移到单元格内,使编号成为可选项。此外,参数\addtag[<label>]
是可选的。它是一个标签名称,仅在需要引用方程时才是必需的。
新代码
\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}
编辑。
注意方程式编号。文档类article
将方程式定义为单个数字,而report
和book
将添加章节以形成组合标签,即(2.1)
只要\chapter
使用即可。如果您希望仅使用即可获得相同的效果article
,请将以下代码添加到序言中:
\counterwithin[\arabic]{equation}{section}
EDIT2.(显示样式)
默认情况下,内联方程式使用文本样式排版,这是一种更紧凑的形式,适合文本之间的表达式。这可以通过附加\displaystyle
到每个表达式来更改。但是,>{...}
可以自动对列中的每个单元格执行此操作。请参阅下面的代码片段。您只需\displaystyle
在后面添加\(
\begin{tabular}{| *2{>{\(\displaystyle}p{5cm}<{\)}|}}
% ...
\end{tabular}
更改后,方程式可能不再适合行。在这种情况下,将拉伸因子增加到\arraystretch
大于 1.8 的值,例如
\renewcommand*{\arraystretch}{1.85}
这是一个简单的解决方案
\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}
答案2
正如 @Rmano 在评论中建议的那样,可以使用Heiko 的回答在这里。要使列正确对齐,您只需使用具有定义宽度的列,而\hfill
不仅仅是不可中断的空间~
。这里有一个建议,代码略有改进:
\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}
顺便说一句:我建议不要使用垂直线和大量水平线。查看包装booktabs
了解有关如何制作漂亮表格的信息。
如果方程编号也取决于表格编号,则可以使用:
\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}
或者在表格下方添加表格标题:
\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}