可自訂的限制和總和符號

可自訂的限制和總和符號

我正在尋找一種方法來定義可客製化極限與求和運算子。目前我有通過定義強/弱限制

\DeclareMathOperator*{\slim}{\mathfrak{s}-lim}
\DeclareMathOperator*{\wlim}{\mathfrak{w}-lim}
\DeclareMathOperator*{\wslim}{\mathfrak{w*}-lim}

但我想要更多的自由,例如$\lim[\mu]$渲染為具有適當限制的μ-lim,以及相同的總和($\sum[\mu]$渲染為μ-Σ)。我怎樣才能實現這個目標?

答案1

這是一個可能的實現,假設您正在加載amsmath.

\documentclass{article}

\usepackage{amsmath}

\makeatletter

\newcommand*{\plim}[1][]{%
   \if\relax\detokenize{#1}\relax
      \def\next{\qopname\relax m{lim}}%
   \else
      \def\next{\qopname\newmcodes@ m{#1-lim}}%
   \fi
   \next
}

\newcommand*{\psum}[1][]{%
   \DOTSB
   \if\relax\detokenize{#1}\relax\else
      \operatorname{#1-}\mkern-\thinmuskip
   \fi
   \sum@\slimits@
}

\makeatother


\begin{document}

\begin{gather*}
\plim_{x\to0} \quad \plim[s]_{x\to0} \quad \plim[\mu]_{x\to0} \\[2ex]
\psum_i a_i \quad \psum[\mu]_i a_i
\end{gather*}

\end{document}

在此輸入影像描述

我選擇了限制的放置方式,以便在出現限制時它們以整個表達式為中心,但始終僅位於總和符號下方(無論之前是什麼)。

個人認為使用起來比較安全新的巨集名稱。當然,沒有人阻止你將上面的程式碼更改為

\renewcommand*{\lim}[1][]{%
   \if\relax\detokenize{#1}\relax
      \def\next{\qopname\relax m{lim}}%
   \else
      \def\next{\qopname\newmcodes@ m{#1-lim}}%
   \fi
   \next
}

\renewcommand*{\sum}[1][]{%
   \DOTSB
   \if\relax\detokenize{#1}\relax\else
      \operatorname{#1-}\mkern-\thinmuskip
   \fi
   \sum@\slimits@
}

請注意,這可能有缺點。

答案2

不要從 Campa 的非常好的答案中竊取任何內容,但建議的程式碼還有改進的空間。

如果您想要一種通用方法來為命令添加前綴\lim\sum根據通用命令定義命令,那麼使用 介面xparse會更容易。

我同意 Campa 的觀點,即限制應集中在整個「前綴限制」以下,但僅限於在\sum類似情況下針對大型業者。

\documentclass{article}
\usepackage{amsmath}
%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later

\NewDocumentCommand{\prelim}{mm}{%
  \operatorname*{#1-{#2}}%
}
\NewDocumentCommand{\preop}{mO{\thinmuskip}m}{%
  \DOTSB\operatorname{#1-}\mspace{-#2}#3%
}

\newcommand{\mulim}{\prelim{\mu}{\lim}}
\newcommand{\xlimsup}{\prelim{x}{\limsup}}

\newcommand{\musum}{\preop{\mu}[6mu]{\sum}}
\newcommand{\mubigcup}{\preop{\mu}{\bigcup}}

\begin{document}

\begin{gather*}
\mulim_{x\to0} f(x) \\
\xlimsup_{x\to\infty} f(x) \\
\musum_{i=1}^{n} a_i \\
\mubigcup_{i\in I}A_i
\end{gather*}

\end{document}

我會避免重新定義\lim\sum。對於偶爾出現的非預先定義運算符,您可以將\prelimand\preop與適當的參數一起使用。此\preop指令有一個中間可選參數,用於改進前綴和符號之間的字距調整,使用 時效果更好\sum,但使用 時則不然\bigcup

在此輸入影像描述

答案3

您也可以使用現有命令,然後重新定義它們以添加一個可選參數來添加前綴。

\documentclass{article}
\usepackage{amsmath}
\let\oldlim\lim
\let\oldsum\sum
\renewcommand*{\lim}[1][]{%
    \def\temp{#1}%
    \ifx\temp\empty
        \oldlim%
    \else
        #1\text{--}\!\oldlim%
    \fi%
}
\renewcommand*{\sum}[1][]{%
    \def\temp{#1}%
    \ifx\temp\empty
        \oldsum%
    \else
        #1\text{--}\!\!\oldsum%
    \fi%
}
\begin{document}
\(\lim_{x\to 0} f(x)\) 
\(\lim[\mu]_{x\to 0} f(x)\)
\[
\lim_{x\to 0} f(x)
\qquad
\lim[\mu]_{x\to 0} f(x)
\]

\(\sum_{n=0}^\infty f(n)\)
\(\sum[\mu]_{n=0}^\infty f(n)\)
\[
\sum_{n=0}^\infty f(n)
\qquad
\sum[\mu]_{n=0}^\infty f(n)
\]
\end{document}

我將間距調整為我認為在這裡看起來不錯的值,但它可以在\lim和的重新定義中輕鬆更改\sum

相關內容