如题,下面是一份MWE和要实现的效果示意图:
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}
\begin{equation}\label{aaa}
a^2+b^2=c^2
\end{equation}
\lipsum[2]
% https://ask.latexstudio.net/ask/question/17466.html
\begin{equation}
\begin{alignedat}{7}\label{bbb}
\frac{1}{\sqrt{2}}&y_1&& \mathcolor{red}{-}{}& \frac{1}{\sqrt{2}}&y_2&& & & && &{}=f_1\\
\frac{1}{\sqrt{2}}&y_1&& \mathcolor{red}{+}{}& \frac{1}{\sqrt{2}}&y_2&& & & && &{}=f_2\\
& && \mathcolor{red}{+}{}& \frac{1}{\sqrt{2}}&y_2&& \mathcolor{red}{+}{}& &y_3&& &{}=f_3\\
& && \mathcolor{red}{+}{}& -\frac{1}{\sqrt{2}}&y_2&& & & && &{}=f_4\\
\frac{1}{\sqrt{2}}&y_1&& & & && \mathcolor{red}{-}{}& &y_3&& &{}=f_5\\
-\frac{1}{\sqrt{2}}&y_1&& & & && & & && &{}=f_6
\end{alignedat}
\end{equation}
\lipsum[2]
\begin{equation}\label{ccc}
a^2+b^2=c^2
\end{equation}
I want to \eqref{aaa} and \eqref{bbb} together with \eqref{ccc}
\end{document}
是否有更简便的方式,在实现「尽可能减少&
数量」(i.e.保持语法的简便)的同时,实现编号和对齐呢?
用 tikz 的 matrix 模块给一种方法(在这种情况下它并非是最优解)
\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\matrix [matrix of math nodes, nodes={anchor=east},]{
\frac{1}{\sqrt{2}}y_1 & - & \frac{1}{\sqrt{2}}y_2 & & = & f_1\\
\frac{1}{\sqrt{2}}y_1 & + & \frac{1}{\sqrt{2}}y_2 & & = & f_2\\
& \textcolor{red}{+} & \frac{1}{\sqrt{2}}y_2 & + \,y_3 & = & f_3\\
& \textcolor{red}{+} & -\frac{1}{\sqrt{2}}y_2 & & = & f_4\\
\frac{1}{\sqrt{2}}y_1 & & & - \,y_3 & = & f_5\\
-\frac{1}{\sqrt{2}}y_1 & & & & = & f_6\\
};
\end{tikzpicture}
\end{equation}
\begin{equation}
a + b =c
\end{equation}
\end{document}
结果如下
其中 [baseline=(current bounding box.center)]
是为了使 matrix 的公式编号垂直居中。
以上使用的是 equation 环境的自动编号,如果想使用手动编号,只需在 tikzpicture 环境后添加\tag{...}
.
该方法对于公式中不同行列等间距输出的情况不是最优解,而对于行列不等间距的情况是比较方便的(因为 matrix 模块可以轻松指定 row sep, column sep
或者在 &
和 \\
后添加增量距离来实现改变行列间距)。
仅仅使用 tabular
来实现, 大概是下面这样:
\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-3]
\begin{equation}
\def\arraystretch{1.7}
\addtolength{\tabcolsep}{-3pt}
\begin{tabular}{rlrllll}
$\frac{1}{\sqrt{2}}y_1$ & $-$ & $\frac{1}{\sqrt{2}}y_2 $ & & & = & $f_1$\\
$\frac{1}{\sqrt{2}}y_1$ & $+$ & $\frac{1}{\sqrt{2}}y_2 $ & & & = & $f_2$\\
& $\textcolor{red}{+}$ & $\frac{1}{\sqrt{2}}y_2 $ & $+$ & $y_3$ & = & $f_3$\\
& $\textcolor{red}{+}$ & $-\frac{1}{\sqrt{2}}y_2$ & & & = & $f_4$\\
$\frac{1}{\sqrt{2}}y_1$ & & & $-$ & $y_3$ & = & $f_5$\\
$-\frac{1}{\sqrt{2}}y_1$ & & & & & = & $f_6$\\
\end{tabular}
\end{equation}
\lipsum[1][4-6]
\end{document}
用 tabular
比 align
之类的直观一点, 就是会多打一些 $
;如果想少打一些 &
, $
之类的符号,可以手搓一个表格数据解析的函数,但是感觉没必要(去用 tabularray
就好了)。