我有一个包含分数的长方体矩阵,我想调整对齐,因为数字重叠了。我该怎么做?示例如下:
\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \cr
B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \cr
C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \cr }
它看起来像这样:
答案1
- 这可能不是最聪明的解决方案。
- 我提供了一步一步的指南来说明我的方法。
\rule
基本上,我使用仅占据垂直空间的隐形物体。
\documentclass{article}
\begin{document}
\begin{equation}
\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \cr
B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \cr
C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \cr }
\end{equation}
% taken from https://www.tug.org/~hvoss/PDF/mathmode.pdf chapter 1.5
\begin{equation}
\bordermatrix{%
& 0 & 1 & 2 \cr
0 & A & B & C \cr
1 & d & e & f \cr
2 & 1 & 2 & 3 \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 0 & 1 & 2 \cr
0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 1000 & 1000 & 1000 \cr
0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 1000 & 1000 & 1000 \cr
0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr
1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 1000 & 1000 & 1000 \cr
0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr
1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{1pt}{18pt} \cr
B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{1pt}{18pt} \cr
C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \rule[-6pt]{1pt}{18pt} \cr
}
\end{equation}
\begin{equation}
\bordermatrix{%
& 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{0pt}{18pt} \cr
B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{0pt}{18pt} \cr
C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \rule[-6pt]{0pt}{18pt} \cr
}
\end{equation}
\end{document}
此外,我建议使用减号大小的不可见空间来美化负分数。
\documentclass{article}
\begin{document}
\section*{Ugly}
\begin{equation}
\bordermatrix{%
& 0 & 1 & 2 \cr
0 & \frac{1}{-1} & \frac{1}{1} & \frac{1}{1} \cr
1 & \frac{1}{1} & \frac{-1}{1} & \frac{1}{1} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
}
\end{equation}
\section*{Nice}
\begin{equation}
\bordermatrix{%
& 0 & 1 & 2 \cr
0 & \frac{\phantom{-}1}{-1} & \frac{1}{1} & \frac{1}{1} \cr
1 & \frac{1}{1} & \frac{-1}{\phantom{-}1} & \frac{1}{1} \cr
2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr
}
\end{equation}
\end{document}
答案2
您可以使用blkarray
包作为边界矩阵,\dfrac
而不是\frac
稍微放大分数。
\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}
\begin{document}
\renewcommand{\arraystretch}{2.5}
\[
\begin{blockarray}{r*8c}
& 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \\
\begin{block}{r\{*8c\}}
A= & \dfrac{1}{8} & \dfrac{1}{8} & 0 & \dfrac{-1}{2} & \dfrac{1}{4} & \dfrac{1}{4} & \dfrac{1}{8} & \dfrac{-3}{8} \\
B= & \dfrac{-1}{4} & \dfrac{-1}{4} & \dfrac{-1}{4} & \dfrac{1}{4} & \dfrac{1}{8} & \dfrac{-7}{8} & \dfrac{1}{8} & \dfrac{-3}{8} \\
C= & \dfrac{1}{8} & \dfrac{1}{8} & \dfrac{1}{4} & \dfrac{1}{4} & \dfrac{-3}{8} & \dfrac{5}{8} & \dfrac{-1}{4} & \dfrac{3}{4} \\[1ex]
\end{block}
\end{blockarray}
\]
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}
编辑:受 Heiko Oberdiek 的回答启发,有了一点改进:
\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}
\newcommand{\myp}{\phantom{-}}
\begin{document}
\renewcommand{\arraystretch}{2.5}
\[
\begin{blockarray}{r*8c<{\hspace{2pt}}}
& 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111\\
\begin{block}{r\{*8c<{\hspace{2pt}}\}}
A= & \myp\dfrac{1}{8} & \myp\dfrac{1}{8} & \myp0 & -\dfrac{1}{2} & \myp\dfrac{1}{4} & \myp\dfrac{1}{4} & \myp\dfrac{1}{8} & -\dfrac{3}{8} \\
B= & -\dfrac{1}{4} & -\dfrac{1}{4} & -\dfrac{1}{4} & \myp\dfrac{1}{4} & \myp\dfrac{1}{8} & -\dfrac{7}{8} & \myp\dfrac{1}{8} & -\dfrac{3}{8} \\
C= & \myp\dfrac{1}{8} & \myp\dfrac{1}{8} & \myp\dfrac{1}{4} & \myp\dfrac{1}{4} & -\dfrac{3}{8} & \myp\dfrac{5}{8} & -\dfrac{1}{4} & \myp\dfrac{3}{4} \\[1ex]
\end{block}
\end{blockarray}
\]
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}
第二次编辑:按照 pzorba75 在其评论中的要求,以下是一个带有宏的解决方案,用于在分子\phantom{-}
为正数时添加分数。
当然,可以通过测试分子和分母符号来改进,我相信一些 TeXpert 应该可以做得更好。
\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}
\usepackage{ifthen}
\newcommand{\myp}{\phantom{-}}
\newcommand{\myfrac}[2]{%
\ifthenelse{#1<0}{% numerator < 0
-\dfrac{\the\numexpr#1*-1\relax}{#2}%
}{% numerator >= 0
\myp\dfrac{#1}{#2}%
}%
}
\begin{document}
\renewcommand{\arraystretch}{2.5}
\[
\begin{blockarray}{r*8c<{\hspace{2pt}}}
& 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111\\
\begin{block}{r\{*8c<{\hspace{2pt}}\}}
A= & \myfrac{1}{8} & \myfrac{1}{8} & \myp0 & \myfrac{-1}{2} & \myfrac{1}{4} & \myfrac{1}{4} & \myfrac{1}{8} & \myfrac{-3}{8} \\
B= & \myfrac{-1}{4} & \myfrac{-1}{4} & \myfrac{-1}{4} & \myfrac{1}{4} & \myfrac{1}{8} & \myfrac{-7}{8} & \myfrac{1}{8} & \myfrac{-3}{8} \\
C= & \myfrac{1}{8} & \myfrac{1}{8} & \myfrac{1}{4} & \myfrac{1}{4} & \myfrac{-3}{8} & \myfrac{5}{8} & \myfrac{-1}{4} & \myfrac{3}{4} \\[1ex]
\end{block}
\end{blockarray}
\]
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}
结果显然与之前相同。
答案3
以下示例展示了使用斜线符号表示“水平”分数的方法。这避免了垂直空间的问题。数字的大小不会减小。
在第二个矩阵中,正分数前面有一个减号的空格,以改善垂直对齐并提高可读性。
\documentclass{article}
\begin{document}
\[
\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A = & 1/8 & 1/8 & 0 & -1/2 & 1/4 & 1/4 & 1/8 & -3/8 \cr
B = & -1/4 & -1/4 & -1/4 & 1/4 & 1/8 & -7/8 & 1/8 & -3/8 \cr
C = & 1/8 & 1/8 & 1/4 & 1/4 & -3/8 & 5/8 & -1/4 & 3/4 \cr }
\]
\[
\def\M{\hphantom{-}}
\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr
A = & \M1/8 & \M1/8 & \M0 & -1/2 & \M1/4 & \M1/4 & \M1/8 & -3/8 \cr
B = & -1/4 & -1/4 & -1/4 & \M1/4 & \M1/8 & -7/8 & \M1/8 & -3/8 \cr
C = & \M1/8 & \M1/8 & \M1/4 & \M1/4 & -3/8 & \M5/8 & -1/4 & \M3/4 \cr }
\]
\end{document}