为什么此多个位置的列对齐会失败?

为什么此多个位置的列对齐会失败?

以下文档中的两个不同块(eq.1& eq.2)几乎完全相同,只是添加了一行(表示矩阵的维度)eq.2(标有蓝色的盒子)。

然而,当我尝试将矩阵维度与相应的矩阵对齐时,

  1. \cdot&后添加了一个额外的空格=
  2. 此外,sigma 的对齐也\sum丢失了。

有人可以详细说明我做错了什么吗?

pdf 输出

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{nccmath}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

\begin{fleqn}
\begin{equation}
    \begin{bmatrix*}
        a_{11} & a_{12} & ... & a_{1n}\\
        a_{21} & a_{22} & ... & a_{2n}\\
        ...\\
        a_{p1} & a_{p2} & ... & a_{pn}
    \end{bmatrix*}
    \cdot
    \begin{bmatrix*}
        b_{11} & b_{12} & ... & b_{1q}\\
        b_{21} & b_{22} & ... & b_{2q}\\
        ...\\
        b_{n1} & b_{n2} & ... & b_{nq}
    \end{bmatrix*}
    =
    \begin{bmatrix*}
        c_{11} & c_{12} & ... & c_{1q}\\
        c_{21} & c_{22} & ... & c_{2q}\\
        ...\\
        c_{p1} & c_{p2} & ... & c_{pq}
    \end{bmatrix*}
    \hfill c_{ij} = \sum_{k = 1}^{n} a_{ik} \cdot b_{kj} \qquad
\end{equation}
\end{fleqn}
\\ \\ \\
\begin{fleqn}
\begin{equation}
\begin{aligned}
    &A\ (p \times n) &&B\ (n \times q) & &C\ (p \times q)\\
    &\begin{bmatrix*}
        a_{11} & a_{12} & ... & a_{1n}\\
        a_{21} & a_{22} & ... & a_{2n}\\
        ...\\
        a_{p1} & a_{p2} & ... & a_{pn}
    \end{bmatrix*}
    \cdot
    &&\begin{bmatrix*}
        b_{11} & b_{12} & ... & b_{1q}\\
        b_{21} & b_{22} & ... & b_{2q}\\
        ...\\
        b_{n1} & b_{n2} & ... & b_{nq}
    \end{bmatrix*}
    =
    & &\begin{bmatrix*}
        c_{11} & c_{12} & ... & c_{1q}\\
        c_{21} & c_{22} & ... & c_{2q}\\
        ...\\
        c_{p1} & c_{p2} & ... & c_{pq}
    \end{bmatrix*}
    \hfill c_{ij} = \sum_{k = 1}^{n} a_{ik} \cdot b_{kj} \qquad
\end{aligned}
\end{equation}
\end{fleqn}

\end{document}

答案1

做你想做的事情的方法:

\documentclass[12pt,a4paper]{article}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{mathtools}
\usepackage{nccmath}

\usepackage{nicematrix}

\begin{document}
\begin{fleqn}
\begin{equation}
    \begin{bNiceMatrix}[first-row]
        \Block{1-4}{A(p\times n)} \\
        a_{11} & a_{12} & ... & a_{1n}\\
        a_{21} & a_{22} & ... & a_{2n}\\
        ...\\
        a_{p1} & a_{p2} & ... & a_{pn}
    \end{bNiceMatrix}
    \cdot
    \begin{bNiceMatrix}[first-row]
        \Block{1-4}{B(n\times p)} \\
        b_{11} & b_{12} & ... & b_{1q}\\
        b_{21} & b_{22} & ... & b_{2q}\\
        ...\\
        b_{n1} & b_{n2} & ... & b_{nq}
    \end{bNiceMatrix}
    =
    \begin{bNiceMatrix}[first-row]
        \Block{1-4}{C(p\times q)} \\
        c_{11} & c_{12} & ... & c_{1q}\\
        c_{21} & c_{22} & ... & c_{2q}\\
        ...\\
        c_{p1} & c_{p2} & ... & c_{pq}
    \end{bNiceMatrix}
    \hfill c_{ij} = \sum_{k = 1}^{n} a_{ik} \cdot b_{kj} \qquad
\end{equation}
\end{fleqn}
\end{document} 

上述代码的输出

答案2

我移动了“&”符号,alignedat并在第一行使用并添加了一些手动间距。

无关,nccmath应加载 mathtools. Also, no need to loadamsmath,因为 nccmath 和 mathtools 都会加载它。如果您的发行版不是太旧(utf8 现在是默认设置),则无需加载 inputenc。

\documentclass[12pt,a4paper]{article}

\usepackage{nccmath}
\usepackage{mathtools}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

\begin{fleqn}
\begin{equation}
    \begin{bmatrix*}
        a_{11} & a_{12} & ... & a_{1n}\\
        a_{21} & a_{22} & ... & a_{2n}\\
        ...\\
        a_{p1} & a_{p2} & ... & a_{pn}
    \end{bmatrix*}
    \cdot
    \begin{bmatrix*}
        b_{11} & b_{12} & ... & b_{1q}\\
        b_{21} & b_{22} & ... & b_{2q}\\
        ...\\
        b_{n1} & b_{n2} & ... & b_{nq}
    \end{bmatrix*}
    =
    \begin{bmatrix*}
        c_{11} & c_{12} & ... & c_{1q}\\
        c_{21} & c_{22} & ... & c_{2q}\\
        ...\\
        c_{p1} & c_{p2} & ... & c_{pq}
    \end{bmatrix*}
    \hfill c_{ij} = \sum_{k = 1}^{n} a_{ik} \cdot b_{kj} \qquad
\end{equation}
\end{fleqn}
\\ \\ \\
\begin{fleqn}
\begin{equation}
\begin{alignedat}[b]{3}
    &\enspace A\ (p \times n) &&\quad\mathrlap{B\ (n \times q)} & &\hspace{1.6em} C\ (p \times q) \\
    &\begin{bmatrix*}
        a_{11} & a_{12} & ... & a_{1n}\\
        a_{21} & a_{22} & ... & a_{2n}\\
        ...\\
        a_{p1} & a_{p2} & ... & a_{pn}
    \end{bmatrix*}
    &&\cdot\begin{bmatrix*}
        b_{11} & b_{12} & ... & b_{1q}\\
        b_{21} & b_{22} & ... & b_{2q}\\
        ...\\
        b_{n1} & b_{n2} & ... & b_{nq}
    \end{bmatrix*}
    & & =
\begin{bmatrix*}
        c_{11} & c_{12} & ... & c_{1q}\\
        c_{21} & c_{22} & ... & c_{2q}\\
        ...\\
        c_{p1} & c_{p2} & ... & c_{pq}
    \end{bmatrix*}
    \enspace\; c_{ij} = \sum_{k = 1}^{n} a_{ik} \cdot b_{kj}
\end{alignedat}
\end{equation}
\end{fleqn}

\end{document}

在此处输入图片描述

相关内容