我想对齐数字 1 和 -1,而不管环境中的符号是什么cases
\documentclass[a4paper, 12pt]{book}
\usepackage{amsmath}
\begin{document}
\subsection{Decision function}
$$
h_{w,b}(x) = g(w^\top x + b)
$$
$$
g(w^\top x + b) = \begin{cases}
1 & \text{if } w^\top x + b > 0\\
-1 & \text{otherwise}
\end{cases}
$$
\end{document}
我试过了\begin{cases*}\end{cases*}
,\begin{cases}[d]\end{cases}
但没有用
答案1
答案2
这是一个采用\hfill
而不是 的解决方案\phantom{-}
。
\documentclass[a4paper, 12pt]{book}
\usepackage{amsmath} % for 'gather*' env.
\begin{document}
\setcounter{chapter}{3} % just for this example
\setcounter{section}{2}
\subsection{Decision function}
\begin{gather*}
h_{w,b}(x) = g(w^{\top\!} x + b) \\
g(w^{\top\!} x + b) =
\begin{cases}
\hfill 1 & \text{if $w^{\top\!} x + b > 0$}\\
-1 & \text{otherwise}
\end{cases}
\end{gather*}
\end{document}
答案3
在 的帮助下,您可以根据需要mathtools
定义 的任意多个变体。在这里,我定义了和 兄弟:cases
casesr
casesr
与 类似cases
,但在左栏右对齐;dcasesr
采用显示风格;casesr*
和dcasesr*
与上面相同,但右列以文本模式排版。
对于您来说,casesr*
这是最好的选择。
\documentclass{article}
\usepackage{mathtools}
\makeatletter
\newcases{casesr}
{\quad}% space between columns
{\hfil$\m@th##$}% left column is right aligned
{$\m@th##$\hfil}% right column is left aligned
{\lbrace}% left delimiter
{.}% right delimiter
\newcases{dcasesr}
{\quad}% space between columns
{\hfil$\m@th\displaystyle{##}$}% left column is right aligned
{$\m@th\displaystyle{##}$\hfil}% right column is left aligned
{\lbrace}% left delimiter
{.}% right delimiter
\newcases{casesr*}
{\quad}% space between columns
{\hfil$\m@th##$}% left column is right aligned
{##\hfil}% right column is left aligned, text mode
{\lbrace}% left delimiter
{.}% right delimiter
\newcases{dcasesr*}
{\quad}% space between columns
{\hfil$\m@th\displaystyle{##}$}% left column is right aligned
{##\hfil}% right column is left aligned, text mode
{\lbrace}% left delimiter
{.}% right delimiter
\makeatother
\begin{document}
\subsection{Decision function}
\begin{gather*}
h_{w,b}(x) = g(w^\top x + b)
\\
g(w^\top x + b) =
\begin{casesr*}
1 & if $w^\top x + b > 0$\\
-1 & otherwise
\end{casesr*}
\end{gather*}
\end{document}
切勿使用$$
,请参阅为什么 \[ ... \] 比 $$ ... $$ 更可取?