我想要一个用于高斯消元法的宏,但我不喜欢高斯包的符号。我找到了一个很棒的宏这里,但我想在一行中包含多个步骤。当我删除 \\ 时,效果并不好,矩阵和行操作之间的间距不相等,并且行不会换行: 此外,我想减少矩阵和行操作之间的间距。如何做到这一点?
来源:
\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\allowdisplaybreaks
\makeatletter
\newcounter{elimination@steps}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash$}p{#1}<{$}}
\def\elimination@num@rights{}
\def\elimination@num@variables{}
\def\elimination@col@width{}
\newenvironment{elimination}[4][0]
{
\setcounter{elimination@steps}{0}
\def\elimination@num@rights{#1}
\def\elimination@num@variables{#2}
\def\elimination@col@width{#3}
\renewcommand{\arraystretch}{#4}
\start@align\@ne\st@rredtrue\m@ne
}
{
\endalign
\ignorespacesafterend
}
\newcommand{\eliminationstep}[2]
{
\ifnum\value{elimination@steps}>0\sim\quad\fi
\left[
\ifnum\elimination@num@rights>0
\begin{array}
{@{}*{\elimination@num@variables}{R{\elimination@col@width}}
|@{}*{\elimination@num@rights}{R{\elimination@col@width}}}
\else
\begin{array}
{@{}*{\elimination@num@variables}{R{\elimination@col@width}}}
\fi
#1
\end{array}
\right]
&
\begin{array}{l}
#2
\end{array}
\addtocounter{elimination@steps}{1}
}
\makeatother
\begin{document}
\begin{elimination}[3]{3}{1.75em}{1.1}
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
4 & -7 & 4 & 0 & 1 & 0 \\
3 & -4 & 2 & 0 & 0 & 1
}
{
\\
-R_{1} \\
-\frac{3}{4} R_{1}
}
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
0 & 1 & -1 & -1 & 1 & 0 \\
0 & 2 & -\frac{7}{4} & -\frac{3}{4} & 0 & 1
}
{
\\
\\
-2R_{2} \\
}
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
0 & 1 & -1 & -1 & 1 & 0 \\
0 & 0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
}
{
\\
\\
+2R_{2} \\
}
\end{elimination}
\end{document}
答案1
如果您在 align 中放置多个 &,它会假定您正在将几个方程放在一起,因此每个其他 & 都会像 \hfil 一样展开。每一步需要 2 个 &。
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\usepackage{array}
\makeatletter
\newcounter{elimination@steps}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash$}p{#1}<{$}}
\def\elimination@num@rights{}
\def\elimination@num@variables{}
\def\elimination@col@width{}
\newenvironment{elimination}[4][0]
{
\setcounter{elimination@steps}{0}
\def\elimination@num@rights{#1}
\def\elimination@num@variables{#2}
\def\elimination@col@width{#3}
\renewcommand{\arraystretch}{#4}
\start@align\@ne\st@rredtrue\m@ne
}
{
\endalign
\ignorespacesafterend
}
\newcommand{\eliminationstep}[2]
{
\ifnum\value{elimination@steps}>0\sim\quad\fi
\left[
\ifnum\elimination@num@rights>0
\begin{array}
{@{}*{\elimination@num@variables}{R{\elimination@col@width}}
|@{}*{\elimination@num@rights}{R{\elimination@col@width}}}
\else
\begin{array}
{@{}*{\elimination@num@variables}{R{\elimination@col@width}}}
\fi
#1
\end{array}
\right]
&
\begin{array}{l}
#2
\end{array}
&% moved second & here
\addtocounter{elimination@steps}{1}
}
\makeatother
\begin{document}
\begin{elimination}[3]{3}{1.1em}{1.1}% Decreased from 1.75em
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
4 & -7 & 4 & 0 & 1 & 0 \\
3 & -4 & 2 & 0 & 0 & 1
}
{
\\
-R_{1} \\
-\frac{3}{4} R_{1}
}
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
0 & 1 & -1 & -1 & 1 & 0 \\
0 & 2 & -\frac{7}{4} & -\frac{3}{4} & 0 & 1
}
{
\\
\\
-2R_{2}
}
\\[10pt]% increased spacing between rows
\eliminationstep
{
4 & -8 & 5 & 1 & 0 & 0 \\
0 & 1 & -1 & -1 & 1 & 0 \\
0 & 0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
}
{
\\
\\
+2R_{2}
}
\end{elimination}
\end{document}