最近我看到这个帖子我觉得我想要一些类似的东西,但是要有数学环境,这样如果我写一行方程式,它就会把它放在中心,就像equation
这样。如果我添加换行符\\
,它应该是左对齐的。
如果可能的话,我也很想能够使用&
它来使列居中,即&
只负责列,而不是对齐。
答案1
在我看来,这并不是一个好的标价。但顾客永远是对的。;-)
环境有一个可选参数(默认 2em)来设置列之间的间距。
\documentclass{article}
\usepackage{amsmath,array}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{mymath}{O{2em}b}
{
% #1 = spacing between columns, #2 = body
\tl_if_in:nnT { #2 } { & }
{
\bool_set_true:N \l__antshar_mymath_ampersand_bool
}
\seq_set_split:Nnn \l__antshar_mymath_body_seq { \\ } { #2 }
\int_compare:nTF { \seq_count:N \l__antshar_mymath_body_seq = 1 }
{
\__antshar_mymath_single:nn { #1 } { #2 }
}
{
\__antshar_mymath_multiple:n { #1 }
}
}
{\ignorespacesafterend}
\seq_new:N \l__antshar_mymath_body_seq
\seq_new:N \l__antshar_mymath_row_seq
\bool_new:N \l__antshar_mymath_ampersand_bool
\int_new:N \l__antshar_mymath_cols_int
\cs_new_protected:Nn \__antshar_mymath_single:nn
{
\bool_if:NTF \l__antshar_mymath_ampersand_bool
{
\seq_set_split:Nnn \l__antshar_mymath_row_seq { & } { #2 }
\[
\begin{array}
{
@{}
>{\displaystyle}c
*{\int_eval:n { \seq_count:N \l__antshar_mymath_row_seq - 1 }}
{@{\hspace{#1}}>{\displaystyle}c}
@{}
}
#2
\end{array}
\]
}
{
\[ #2 \]
}
}
\cs_new_protected:Nn \__antshar_mymath_multiple:n
{
\bool_if:NTF \l__antshar_mymath_ampersand_bool
{
% count the maximum number of columns
\int_zero:N \l__antshar_mymath_cols_int
\seq_map_inline:Nn \l__antshar_mymath_body_seq
{
\seq_set_split:Nnn \l__antshar_mymath_row_seq { & } { ##1 }
\int_set:Nn \l__antshar_mymath_cols_int
{
\int_max:nn { \l__antshar_mymath_cols_int } { \seq_count:N \l__antshar_mymath_row_seq }
}
}
\[
\begin{array}
{
@{}
>{\displaystyle}c
*{\int_eval:n { \l__antshar_mymath_cols_int - 1 } }
{@{\hspace{#1}}>{\displaystyle}c}
@{}
}
\seq_use:Nn \l__antshar_mymath_body_seq { \\\noalign{\vspace{\jot}} }
\end{array}
\]
}
{
\seq_set_map:NNn \l__antshar_mymath_row_seq \l__antshar_mymath_body_seq
{
& \exp_not:n { {##1} } &&
}
\begin{flalign*}
\seq_use:Nn \l__antshar_mymath_row_seq { \\ }
\end{flalign*}
}
}
\ExplSyntaxOff
\begin{document}
\noindent Some text in front of the display, just for separation
\begin{mymath}
a=b
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
a=b & c=d & e=ffffffff
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
a=b & c=d & e=ffffffff \\
1=1 & 120\ne1000 & u
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}[4em]
a=b & c=d & e=ffffffff
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
abc+def\\
=x+y
\end{mymath}
\end{document}
图像已添加制作完成showframe
。
添加
环境mymath*
把一切都放在左边。
\documentclass{article}
\usepackage{amsmath,array}
\usepackage{xparse}
\usepackage{showframe}
\ExplSyntaxOn
\NewDocumentEnvironment{mymath}{O{2em}b}
{
\bool_set_false:N \l__antshar_mymath_left_bool
\antshar_mymath_main:nn { #1 } { #2 }
}
{\ignorespacesafterend}
\NewDocumentEnvironment{mymath*}{O{2em}b}
{
\bool_set_true:N \l__antshar_mymath_left_bool
\antshar_mymath_main:nn { #1 } { #2 }
}
{\ignorespacesafterend}
\seq_new:N \l__antshar_mymath_body_seq
\seq_new:N \l__antshar_mymath_row_seq
\bool_new:N \l__antshar_mymath_ampersand_bool
\bool_new:N \l__antshar_mymath_left_bool
\int_new:N \l__antshar_mymath_cols_int
\cs_new_protected:Nn \antshar_mymath_main:nn
{
% #1 = spacing between columns, #2 = body
\tl_if_in:nnT { #2 } { & }
{
\bool_set_true:N \l__antshar_mymath_ampersand_bool
}
\seq_set_split:Nnn \l__antshar_mymath_body_seq { \\ } { #2 }
\int_compare:nTF { \seq_count:N \l__antshar_mymath_body_seq = 1 }
{
\__antshar_mymath_single:nn { #1 } { #2 }
}
{
\__antshar_mymath_multiple:n { #1 }
}
}
\cs_new_protected:Nn \__antshar_mymath_single:nn
{
\bool_if:NTF \l__antshar_mymath_ampersand_bool
{
\seq_set_split:Nnn \l__antshar_mymath_row_seq { & } { #2 }
\[
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{0pt} }
\begin{array}
{
@{}
>{\displaystyle}c
*{\int_eval:n { \seq_count:N \l__antshar_mymath_row_seq - 1 }}
{@{\hspace{#1}}>{\displaystyle}c}
@{}
}
#2
\end{array}
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{1000pt minus 1fill} }
\]
}
{
\[
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{0pt} }
#2
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{1000pt minus 1fill} }
\]
}
}
\cs_new_protected:Nn \__antshar_mymath_multiple:n
{
\bool_if:NTF \l__antshar_mymath_ampersand_bool
{
% count the maximum number of columns
\int_zero:N \l__antshar_mymath_cols_int
\seq_map_inline:Nn \l__antshar_mymath_body_seq
{
\seq_set_split:Nnn \l__antshar_mymath_row_seq { & } { ##1 }
\int_set:Nn \l__antshar_mymath_cols_int
{
\int_max:nn { \l__antshar_mymath_cols_int } { \seq_count:N \l__antshar_mymath_row_seq }
}
}
\[
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{0pt} }
\begin{array}
{
@{}
>{\displaystyle}c
*{\int_eval:n { \l__antshar_mymath_cols_int - 1 } }
{@{\hspace{#1}}>{\displaystyle}c}
@{}
}
\seq_use:Nn \l__antshar_mymath_body_seq { \\\noalign{\vspace{\jot}} }
\end{array}
\bool_if:NT \l__antshar_mymath_left_bool { \hspace{1000pt minus 1fill} }
\]
}
{
\[
\hspace{0pt}
\begin{array}{@{}>{\displaystyle}l@{}}
\seq_use:Nn \l__antshar_mymath_body_seq { \\\noalign{\vspace{\jot}} }
\end{array}
\hspace{1000pt minus 1fill}
\]
}
}
\ExplSyntaxOff
\begin{document}
\noindent Some text in front of the display, just for separation
\begin{mymath}
a=b
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
a=b & c=d & e=ffffffff
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
a=b & c=d & e=ffffffff \\
1=1 & 120\ne1000 & u
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}[4em]
a=b & c=d & e=ffffffff
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath}
abc+def\\
=x+y
\end{mymath}
Some text in front of the display, just for separation
\begin{mymath*}
a=b
\end{mymath*}
Some text in front of the display, just for separation
\begin{mymath*}
a=b & c=d & e=ffffffff
\end{mymath*}
Some text in front of the display, just for separation
\begin{mymath*}
a=b & c=d & e=ffffffff \\
1=1 & 120\ne1000 & u
\end{mymath*}
Some text in front of the display, just for separation
\begin{mymath*}[4em]
a=b & c=d & e=ffffffff
\end{mymath*}
Some text in front of the display, just for separation
\begin{mymath*}
abc+def\\
=x+y
\end{mymath*}
\end{document}