数组对齐问题

数组对齐问题

我遇到了数组对齐的问题。

请找到以下代码:

\documentclass[12 pt, a4paper]{book}
\usepackage{multicol}
\usepackage{geometry}
\geometry{
          a4paper,
          total={170 mm,257 mm},
          left=20 mm,
          top=20 mm,
         }
\usepackage{amsmath,bm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{arydshln}

\setlength{\parindent}{2em}

\newcommand{\wt}[2]{\underset{\substack{\textstyle\uparrow\\\hidewidth\mathstrut#2\hidewidth}}{#1}}
\definecolor{mygray}{gray}{0.6}

\begin{document}
  \textbf{Solution :}\\
    \[
    \left.
    \hspace{1 cm}
    \begin{array}{cccccc}
               &   & 1 & 0 & 1 & 0 \\
        \times &   &   & 1 & 0 & 1 \\
        \hline
               & \underline{1} & 1 & 0 & 1 & 0 \\
               & 0 & 0 & 0 & 0 &   \\
             1 & 0 & 1 & 0 &   &   \\
        \hline
             1 & 1 & 0 & 0 & 1 & 0 \\
    \end{array}
    \right. \hspace{0.25 cm}\vline \hspace{0.1 cm} \text{\parbox[t]{13 cm}{As, the product of binary multiplication would always be less than 2. It won't generate any carry.}}
    \begin{array}{ccccccccc}
        & & \textbf{Product/Sum} & & \textbf{Base} & & \textbf{Carry} & & \textbf{Result} \\
        1 + 0 + 1 & = & 2 > 1 & = & 2 & \times & 1 & + & 0 \\[0.2 cm]   
    \end{array}
    \]
\end{document}

得到的输出如下:

获得的输出

正如我们在图片中看到的,第二个数组与文本右侧对齐。我希望该数组位于文本下方。

期望的输出如下: 期望输出

有人能帮我解决这个问题吗?

答案1

可能不太优雅,因为我使用嵌套表格来定位数学元素、文本和线条。

在此处输入图片描述

\documentclass[12 pt, a4paper]{book}
\usepackage{multicol}
\usepackage{geometry}
\geometry{
          a4paper,
          total={170 mm,257 mm},
          left=20 mm,
          top=20 mm,
         }
\usepackage{amsmath,bm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{arydshln}

\setlength{\parindent}{2em}

\newcommand{\wt}[2]{\underset{\substack{\textstyle\uparrow\\\hidewidth\mathstrut#2\hidewidth}}{#1}}
\definecolor{mygray}{gray}{0.6}

\usepackage{calc}

\begin{document}
  \textbf{Solution :}
  
\begin{tabular}{p{0.25\textwidth-2\tabcolsep-\arrayrulewidth}|p{0.75\textwidth-2\tabcolsep}}
        \(\begin{array}[t]{cccccc}
               &   & 1 & 0 & 1 & 0 \\
        \times &   &   & 1 & 0 & 1 \\
        \hline
               & \underline{1} & 1 & 0 & 1 & 0 \\
               & 0 & 0 & 0 & 0 &   \\
             1 & 0 & 1 & 0 &   &   \\
        \hline
             1 & 1 & 0 & 0 & 1 & 0 \\
    \end{array}\)
    &
    \begin{tabular}[t]{@{}p{0.75\textwidth-2\tabcolsep}@{}}
    As, the product of binary multiplication would always be less than 2. It won't generate any carry. \\
    \hdashline   
    \[\begin{array}{ccccccccc}
        & & \textbf{Product/Sum} & & \textbf{Base} & & \textbf{Carry} & & \textbf{Result} \\
        1 + 0 + 1 & = & 2 > 1 & = & 2 & \times & 1 & + & 0 \\[0.2 cm]   
    \end{array}\]
    \end{tabular}
\end{tabular}

\end{document}

答案2

为了不用猜测右侧材料的宽度,可以使用并在其中tabularx嵌套。arraytabular

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath,bm}
\usepackage{tabularx,array}
\usepackage{arydshln}

\geometry{
  a4paper,
  total={170 mm,257 mm},
  left=20 mm,
  top=20 mm,
}

\begin{document}

\noindent\textbf{Solution:}
\[
\begin{tabularx}{\textwidth}{@{} c | X @{}}
$\begin{array}{cccccc}
         &   & 1 & 0 & 1 & 0 \\
  \times &   &   & 1 & 0 & 1 \\
  \hline
         & \underline{1} & 1 & 0 & 1 & 0 \\
         & 0 & 0 & 0 & 0 &   \\
       1 & 0 & 1 & 0 &   &   \\
  \hline
       1 & 1 & 0 & 0 & 1 & 0 \\
\end{array}$ &
\begin{tabular}{@{}p{\linewidth}@{}}
As the product of binary multiplication would always be less than~$2$,
it won't generate any carry.
\\[2ex]
\hdashline
\[\begin{array}{ccccccccc}
  & & \textbf{Product/Sum} & & \textbf{Base} & & \textbf{Carry} & & \textbf{Result} \\
  1 + 0 + 1 & = & 2 > 1 & = & 2 & \times & 1 & + & 0 \\[0.2 cm]   
\end{array}\]
\end{tabular}
\end{tabularx}
\]

\end{document}

在此处输入图片描述

答案3

我提出了这个变体代码,也基于 tabularx,但使用matrix环境和\Longstack虚线下的部分:

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath,bm}
\usepackage{tabularx}
\usepackage{arydshln, booktabs,}
\usepackage[usestackEOL]{stackengine}

\geometry{a4paper, total={170 mm,257 mm}, left=20 mm, top=20 mm}

\begin{document}

\noindent\textbf{Solution:}
\[
\begin{tabularx}{\textwidth}{@{} c | X @{}}
$\begin{matrix}
         & & 1 & 0 & 1 & 0 \\
  \times & & & 1 & 0 & 1 \\
  \midrule
         & ^{\underline{1}} & 1 & 0 & 1 & 0 \\
         & 0 & 0 & 0 & 0 & \\
       1 & 0 & 1 & 0 & & \\
  \midrule
       1 & 1 & 0 & 0 & 1 & 0 \\
\end{matrix}$\enspace &
\begin{tabular}{@{}p{\linewidth}@{}}
As the product of binary multiplication would always be less than~$2$,
it won't generate any carry.
\\[2ex]
\hdashline
\stackMath
\[ 1 + 0 + 1 = \Longstack{\textbf{Product/Sum} \\ 2 > 1} = \Longstack{ \textbf{Base}\\ 2} \times \Longstack{\textbf{Carry}\\ 1}
  + \Longstack{\\\textbf{Result}\\0} \]
\end{tabular}
\end{tabularx}
\]

\end{document} 

在此处输入图片描述

相关内容