
我不知道這是否真的是一個好主意,但我想知道是否可以實現一個\frac
可以在對齊選項卡 ( ) 上對齊分子和分母的版本&
。
該網站上有很多關於分數對齊的問題,在許多情況下最明顯的解決方案是使用\hfill
.但考慮一下這樣的分數:
在我看來,最上面的一個看起來有點混亂,但使用將\hfill
分子向左移動得太遠。我真正想要的(我認為)是將它們精確地對齊在開括號中。所以我想像一個\alignedfrac
命令可以讓我寫
\[
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)}
\]
並將分子和分母與&
字元對齊。
我知道這實際上可能是也可能不是一個好的選擇,而且我可能正在解決一個非問題,但我仍然很好奇是否有可能實現這樣的事情。
這是生成上面圖像的 MWE:
\documentclass{article}
\begin{document}
\noindent without any alignment:
\[
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using hfill:
\[
\frac{q(x\mid y)\hfill}{p_\mathrm{foo}(w\mid x,y,z)}
\]
\end{document}
答案1
在這裡,我獨立建構了 frac ( \leftfrac
) 的左側部分和右側部分 ( \rightfrac
),使用\hfill
s 來實現部分調整。然後,我將兩者相鄰排版,\mkern
中間有一個負片。
根據 Mico 的建議,出現的|
已替換為\mid
, 以獲得更好的外觀。
編輯以添加\vphantom
匹配,我相信如果左右部分具有不同的垂直範圍,這將有所幫助。
\documentclass{article}
\begin{document}
\newcommand\alignedfrac[2]{%
\leftfrac#1\\#2\relax\mkern-4.5mu\rightfrac#1\\#2\relax
}
\def\leftfrac#1\\#3\relax{%
\frac{\hfill#1\vphantom{#2}}{\hfill#3\vphantom{#4}}}
\def\rightfrac#1\\#3\relax{%
\frac{\vphantom{#1}#2\hfill}{\vphantom{#3}#4\hfill}}
\noindent without any alignment:
\[
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using hfill:
\[
\frac{q(x\mid y)\hfill}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using alignedfrac
\[
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)}
\]
\end{document}
答案2
你能行的:
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\alignedfrac}[2]{%
\begingroup
\setbox0=\vbox{%
\m@th
\ialign{&$\displaystyle##$\cr#1\cr#2\cr}%
\setbox2=\lastbox
\hbox{%
\unhbox2
\unskip
\setbox4=\lastbox
\unskip
\setbox6=\lastbox
\global\dimen1=\wd6
\global\dimen3=\wd4
}
}
\frac{\af@make#1\@nil}{\af@make#2\@nil}
\endgroup
}
\def\af@make#1\@nil{%
\makebox[\dimen1][r]{$\displaystyle#1$}%
\makebox[\dimen3][l]{$\displaystyle#2$}%
}
\makeatother
\begin{document}
\begin{gather*}
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)} \\
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\end{gather*}
\end{document}
這是怎麼回事?我與基元對齊\halign
,然後通過拆卸構建的盒子來檢查它的最後一行。我存放這兩個部分的尺寸,並用它們來製作寬度合適的盒子。
然而,正如圖像清楚顯示的那樣,我認為標準分數沒有任何改進。