我想排版以逗号分隔的有序对,并以\langle
和\rangle
作为分隔符。我希望在内联数学模式下,有序对的换行符位于逗号处。
因此我想出了下面这段代码,但是其中有一个烦人的错误。
\documentclass{article}
%\usepackage{amsmath,physics}
\def\opair#1{%
\mathopen\langle
\kern 2pt
\begingroup
\edef\@tempa{#1,}%
\expandafter
\endgroup
\expandafter\opairImpl\@tempa\relax
\kern 2pt
\mathclose\rangle
}
\def\opairImpl#1,#2\relax{%
{#1}%
\begingroup
\ifx\relax#2\relax
\def\next{ \endgroup }%
\else
\def\next{
\endgroup
,\,\allowbreak
\opairImpl#2\relax
}
\fi
\next
}
\begin{document}
\( \opair{x , y, z} \)
\( \opair{ \cos\alpha, \cos\beta, \cos\gamma } \)
\( \opair{x_1,x_2,\cdots{},x_n} \)
\(\opair{a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z}\)
\end{document}
当\usepackage{amsmath,physics}
禁用时,
\( \opair{x_1,x_2,\cdots{},x_n} \)
会产生可怕的输出,后面的逗号x_2
会丢失,如下面的屏幕截图所示。
启用后\usepackage{amsmath,physics}
,出现了一个更烦人的 bug。日志文件如下所示。
This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020/W32TeX) (preloaded format=xelatex 2021.3.15) 11 APR 2021 22:34
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**tmp.tex
(./tmp.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-03-06>
(~/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(~/texlive/2020/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2019/12/20 v1.4l Standard LaTeX file (size option)
)
\c@part=\count163
\c@section=\count164
\c@subsection=\count165
\c@subsubsection=\count166
\c@paragraph=\count167
\c@subparagraph=\count168
\c@figure=\count169
\c@table=\count170
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen134
)
(~/texlive/2020/texmf-dist/tex/latex/amsmath/amsmath.sty
Package: amsmath 2020/01/20 v2.17e AMS math features
\@mathmargin=\skip49
For additional information on amsmath, use the `?' option.
(~/texlive/2020/texmf-dist/tex/latex/amsmath/amstext.sty
Package: amstext 2000/06/29 v2.01 AMS text
(~/texlive/2020/texmf-dist/tex/latex/amsmath/amsgen.sty
File: amsgen.sty 1999/11/30 v2.0 generic functions
\@emptytoks=\toks15
\ex@=\dimen135
))
(~/texlive/2020/texmf-dist/tex/latex/amsmath/amsbsy.sty
Package: amsbsy 1999/11/29 v1.2d Bold Symbols
\pmbraise@=\dimen136
)
(~/texlive/2020/texmf-dist/tex/latex/amsmath/amsopn.sty
Package: amsopn 2016/03/08 v2.02 operator names
)
\inf@bad=\count171
LaTeX Info: Redefining \frac on input line 227.
\uproot@=\count172
\leftroot@=\count173
LaTeX Info: Redefining \overline on input line 389.
\classnum@=\count174
\DOTSCASE@=\count175
LaTeX Info: Redefining \ldots on input line 486.
LaTeX Info: Redefining \dots on input line 489.
LaTeX Info: Redefining \cdots on input line 610.
\Mathstrutbox@=\box45
\strutbox@=\box46
\big@size=\dimen137
LaTeX Font Info: Redeclaring font encoding OML on input line 733.
LaTeX Font Info: Redeclaring font encoding OMS on input line 734.
\macc@depth=\count176
\c@MaxMatrixCols=\count177
\dotsspace@=\muskip16
\c@parentequation=\count178
\dspbrk@lvl=\count179
\tag@help=\toks16
\row@=\count180
\column@=\count181
\maxfields@=\count182
\andhelp@=\toks17
\eqnshift@=\dimen138
\alignsep@=\dimen139
\tagshift@=\dimen140
\tagwidth@=\dimen141
\totwidth@=\dimen142
\lineht@=\dimen143
\@envbody=\toks18
\multlinegap=\skip50
\multlinetaggap=\skip51
\mathdisplay@stack=\toks19
LaTeX Info: Redefining \[ on input line 2859.
LaTeX Info: Redefining \] on input line 2860.
)
(~/texlive/2020/texmf-dist/tex/latex/physics/physics.sty
Package: physics
(~/texlive/2020/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(~/texlive/2020/texmf-dist/tex/latex/l3kernel/expl3.sty
Package: expl3 2020-03-06 L3 programming layer (loader)
(~/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-xdvipdfmx.def
File: l3backend-xdvipdfmx.def 2020-03-12 L3 backend support: xdvipdfmx
\g__graphics_track_int=\count183
\l__pdf_internal_box=\box47
\g__pdf_backend_object_int=\count184
\g__pdf_backend_annotation_int=\count185
))
Package: xparse 2020-03-06 L3 Experimental document command parser
\l__xparse_current_arg_int=\count186
\g__xparse_grabber_int=\count187
\l__xparse_m_args_int=\count188
\l__xparse_v_nesting_int=\count189
))
No file tmp.aux.
\openout1 = `tmp.aux'.
LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for OMS/cmsy/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for TS1/cmr/m/n on input line 31.
LaTeX Font Info: Trying to load font information for TS1+cmr on input line 3
1.
(~/texlive/2020/texmf-dist/tex/latex/base/ts1cmr.fd
File: ts1cmr.fd 2019/12/16 v2.5j Standard LaTeX font definitions
)
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for TU/lmr/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 31.
LaTeX Font Info: ... okay on input line 31.
! Undefined control sequence.
\DN@ ->\def \next@
l.32 \( \opair{x_1,x_2,\cdots{},x_n}
\)
Here is how much of TeX's memory you used:
1999 strings out of 479418
31789 string characters out of 5884585
281504 words of memory out of 5000000
19574 multiletter control sequences out of 15000+600000
532800 words of font info for 27 fonts, out of 8000000 for 9000
1348 hyphenation exceptions out of 8191
44i,3n,40p,309b,52s stack positions out of 5000i,500n,10000p,200000b,80000s
No pages of output.
我注意到\( \opair{x_1,x_2, \cdots{},x_n} \)
确保前面的逗号\cdots{}
正确显示,与\( \opair{x_1,x_2,\cdots{},x_n} \)
; 相反,但我不知道为什么这样。
那我该怎么办?请帮忙,提前谢谢。
编辑:
预期输出:
预期行为:
与 amsmath 和 physics 等包一起使用时无错误。
编辑1:
采用 @egrep 的方法后,以下代码片段出现未定义的控制序列错误:
\begin{align*}
\mat{T}
&=\opair{
\eval{\jacobi{F,G}{y,z}}_M,
\eval{\jacobi{F,G}{z,x}}_M,
\eval{\jacobi{F,G}{x,y}}_M
} \\
&=\opair{
\begin{vmatrix}
F_y & F_z \\
G_y & G_z \\
\end{vmatrix}_{M},
\begin{vmatrix}
F_z & F_x \\
G_z & G_x \\
\end{vmatrix}_{M},
\begin{vmatrix}
F_x & F_y \\
G_x & G_y \\
\end{vmatrix}_{M}
}.
\end{align*}
日志档案:
! Argument of \__clist_use:nwwwwnwn has an extra }.
<inserted text>
\par
l.42 \end{align*}
?
顺便说一下我定义\let\mat\boldsymbol
和\newcommand{\jacobi}[2]{ \pdv{ (#1) }{ (#2) } }%
预期输出:
我认为这是的错误\jacobi
,但我不知道该怎么办。
答案1
出现错误是\edef\@tempa{#1,}
因为\cdots
(在该amsmath
版本中)没有幸存下来。
没有必要,\edef
而且\def
就足够了。
这是一个与您的没有实质性区别的替代实现,只是我使用 mu 单位作为前导和尾随空格,并且在逗号后的粘合处添加了一些小的灵活性以允许对齐。
我没有重新发明轮子来处理逗号列表,而是使用了 中的预定义方法expl3
。如果你运行的是 2020-10-01 之前的 LaTeX 版本,则需要\usepackage{xparse}
。
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\opair}{m}
{
\langle\mspace{4mu}
\clist_set:Nn \l_tmpa_clist { #1 }
\clist_use:Nn \l_tmpa_clist {,\mspace{3mu plus 1mu minus 1mu}\allowbreak}
\mspace{4mu}\rangle
}
\ExplSyntaxOff
\begin{document}
\( \opair{x , y, z} \)
\( \opair{ \cos\alpha, \cos\beta, \cos\gamma } \)
\( \opair{x_1,x_2,\cdots,x_n} \)
\(\opair{a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,
z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z}\)
\end{document}
不要使用{}
它来终止数学模式下的命令,因为它可能会添加不需要的空原子。
不同的实现只允许在第二个项目之后换行,但只允许到倒数第二个项目:您不希望有序对跨行断开,但可能希望更长的元组跨行断开。
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\opair}{m}
{
\langle\mspace{4mu}
\kaiserkatze_opair:n { #1 }
\mspace{4mu}\rangle
}
\seq_new:N \l__kaiserkatze_opair_items_seq
\tl_new:N \l__kaiserkatze_opair_first_tl
\cs_new_protected:Nn \kaiserkatze_opair:n
{
\seq_set_from_clist:Nn \l__kaiserkatze_opair_items_seq { #1 }
% set off the first item and insert it
\seq_pop_left:NN \l__kaiserkatze_opair_items_seq \l__kaiserkatze_opair_first_tl
\tl_use:N \l__kaiserkatze_opair_first_tl
% now insert commas and allowed breaks
\seq_map_indexed_inline:Nn \l__kaiserkatze_opair_items_seq
{
% comma and thin space
,\mspace{3mu plus 1mu minus 1mu}
% but \allowbreak only after the second item and before the penultimate
\int_compare:nT { 1 < ##1 < \seq_count:N \l__kaiserkatze_opair_items_seq }
{ \allowbreak }
% the item
##2
}
}
\ExplSyntaxOff
\begin{document}
\( \opair{x , y, z} \)
\( \opair{ \cos\alpha, \cos\beta, \cos\gamma } \)
\( \opair{x_1,x_2,\cdots{},x_n} \)
\(\opair{a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,
z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z}\)
\parbox[t]{0pt}{
\(\opair{a,b,c,d,e,f,g,h}\)
}
\end{document}
只是\parbox
为了显示哪里允许休息。