如何改变此命令以使其在更多情况下发挥作用?

如何改变此命令以使其在更多情况下发挥作用?

因此命令如下:

\newcommand{\sumi1ton}{\sum\limits_{i=1}^{n}}

我希望能够进一步概括这一点。例如,如果我输入 \sumj3toQ,那么它会给出从 j=3 到 Q 的总和。我该如何实现这一点,而无需为每种可能的情况编写命令?

答案1

使用空格作为分隔符以使其更清晰:

自定义总和

\documentclass{article}
\def\sumx #1 #2 to #3{\sum\limits_{#1=#2}^{#3}}
\begin{document}
    \[\sumx i 1 to n\]
    \[\sumx j 3 to Q\]
\end{document}

答案2

这些奇怪的用例够多了吗?不过,空参数的默认行为可以改变。

我添加了一个特殊情况,即您只使用两个参数,to中间有一个。在这种情况下,假定您的意思是\sumx{#1}{0}{#2},您可以在标有的行处更改默认开始(上例中为 0)% CHANGE DEFAULT START HERE

编辑:允许和解析=包含在内,#1就好像#1和都#2给出了一样。

=编辑:还允许在#1和之间进行选择#2

\documentclass[]{article}

\makeatletter
\newcommand\sumx%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@a}% if \sumx is followed by
      {\sumx@spaces@a}%
  }%<<<
\newcommand\sumx@ifempty[1]%>>>
  {%
    \if\relax\detokenize{#1}\relax
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }%<<<
\newcommand\sumx@if@equal@contained@a[1]%>>>
  {%
    \sumx@if@equal@contained@b#1=\end
  }%<<<
\newcommand\sumx@if@equal@contained@b{}%>>>
\def\sumx@if@equal@contained@b#1=#2\end
  {%
    \sumx@ifempty{#2}{\@secondoftwo}{\@firstoftwo}%
  }%<<<
\newcommand\sumx@braces@a[1]%>>>
  {%
    \sumx@after@a{#1}%
  }%<<<
\newcommand\sumx@spaces@a{}%>>>
\def\sumx@spaces@a #1
  {%
    \sumx@after@a{#1}%
  }%<<<
\newcommand\sumx@after@a[1]%>>>
  {%
    \sumx@if@equal@contained@a{#1}
      {\sumx@after@b{#1}{}}
      {%
        \@ifnextchar{t}
          {\sumx@to@a@after@a{#1}}
          {%
            \@ifnextchar{=}
              {\sumx@equal@after@a{#1}}
              {\sumx@after@after@a{#1}}
          }%
      }%
  }%<<<
\newcommand\sumx@equal@after@a{}%>>>
\def\sumx@equal@after@a #1=%
  {%
    \sumx@after@after@a{#1}%
  }%<<<
\newcommand\sumx@after@after@a[1]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@b{#1}}
      {\sumx@spaces@b{#1}}%
  }%<<<
\newcommand\sumx@to@a@after@a{}%>>>
\def\sumx@to@a@after@a #1t%
  {%
    \@ifnextchar{o}
      {\sumx@to@b@after@a{#1}}
      {\sumx@braces@b{#1}{t}}%
  }%<<<
\newcommand\sumx@to@b@after@a{}%>>>
\def\sumx@to@b@after@a #1o%
  {%
    \sumx@to@c@after@a{#1}{0}% CHANGE DEFAULT START HERE
  }%<<<
\newcommand\sumx@to@c@after@a[2]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {\sumx@spaces@c{#1}{#2}}%
  }%<<<
\newcommand\sumx@braces@b[2]%>>>
  {%
    \sumx@after@b{#1}{#2}%
  }%<<<
\newcommand\sumx@spaces@b{}%>>>
\def\sumx@spaces@b #1#2
  {%
    \sumx@after@b{#1}{#2}%
  }%<<<
\newcommand\sumx@after@b[2]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {%
        \@ifnextchar{t}
          {\sumx@to@a{#1}{#2}}
          {\sumx@spaces@c{#1}{#2}}%
      }%
  }%<<<
\newcommand\sumx@braces@c[3]%>>>
  {%
    \sumx@output{#1}{#2}{#3}%
  }%<<<
\newcommand\sumx@spaces@c{}%>>>
\def\sumx@spaces@c #1#2#3
  {%
    \sumx@output{#1}{#2}{#3}%
  }%<<<
\newcommand\sumx@to@a{}%>>>
\def\sumx@to@a #1#2t%
  {%
    \@ifnextchar{o}
      {\sumx@to@b{#1}{#2}}
      {\sumx@output{#1}{#2}{t}}%
  }%<<<
\newcommand\sumx@to@b{}%>>>
\def\sumx@to@b #1#2o%
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {\sumx@spaces@c{#1}{#2}}%
  }%<<<
\newcommand\sumx@output[3]%>>>
  {%
    \sum
    \sumx@ifempty{#1#2#3}% if all arguments are empty
      {}% do nothing more
      {%
        \limits
        \sumx@ifempty{#1#2}% if both 1 and 2 are empty
          {}% do nothing here
          {%
            _{%
              \sumx@ifempty{#1}
                {#2}
                {%
                  \sumx@ifempty{#2}
                    {#1}
                    {#1=#2}%
                }%
            }%
          }%
        \sumx@ifempty{#3}{}{^{#3}}
      }
  }%<<<
\makeatother


\begin{document}
$
\sumx{ij}{10}{30}
\sumx{ij}{10}to{30}
\sumx  ij   10  to  30
\sumx  ij   10      30
\sumx {ij} {10}    {30}
\sumx {ij} {10} to {30}
\sumx {ij}  10      30 
\sumx  ij  {10}     30
\sumx  ij   10     {30}$\par
$
\sumx i 1 t %no o follows the t so it is the third argument
\sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
\sumx{}{1}{10}
\sumx{i}{}{}
\sumx{i}{}{10}
\sumx i t a
\sumx i to a
\sumx i t o a
\sumx i t{o} a % if you really need t and o as two arguments group one or both
\sumx i=1 to a
\sumx i = 1 to a
$
\end{document}

sumx 的多种用途

%>>>只是%<<<我的 VIM 的折叠标记,你可以忽略它们。

相关内容