Typeset saturation function
Using a technique of egreg at Define macro that scans ahead for super- and/or subscript, "absorbs" them and moves their argument, I let xparse
help with redefining ^
and _
to handle the kerning.
This MWE uses the saturation.pdf
supplied by the OP's code.
\documentclass{article}
\usepackage{xparse,mathtools}
\NewDocumentCommand{\sat}{e{^_}}{%
\mathop{\IfValueT{#1}{\phantom{\scriptscriptstyle#1}\mathchoice
{\kern-5pt}{\kern-5pt}{\kern-3pt}{\kern-2pt}}%
\rawsat\mathchoice
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern14mu}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-13mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern14mu\strut}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-13mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern11mu\mathstrut}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-9mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern9mu}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-6mu#2}}}%
}}
\usepackage{scalerel,graphicx}
\newcommand\rawsat{\ThisStyle{\kern.5\LMpt
\includegraphics[height=2\LMex]{saturation}\kern.5\LMpt}}
\begin{document}
$x=\sat_{-5}^{10} u(t)$
$\scriptstyle x=\sat_{-5}^{10} u(t)$
$\scriptscriptstyle x= \sat_{-5}^{10} u(t)$
\end{document}
And just to prove I have resolved the kerning even as the superscript width changes, here is the same example with a wider superscript:
ADDENDUM:
Here is a version that does not require an external image file to be dragged around, but instead saves the tikz
image of it at initialization.
\documentclass{article}
\usepackage{xparse,mathtools,stackengine,tikz,scalerel}
\NewDocumentCommand{\sat}{e{^_}}{%
\mathop{\IfValueT{#1}{\phantom{\scriptscriptstyle#1}\mathchoice
{\kern-5pt}{\kern-5pt}{\kern-3pt}{\kern-2pt}}%
\rawsat\mathchoice
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern15mu}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-14mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern15mu\strut}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-14mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern12mu\mathstrut}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-10mu#2}}}%
{^{\IfValueT{#1}{\mathllap{\scriptscriptstyle#1\mkern10mu}}}%
_{\IfValueT{#2}{\scriptscriptstyle\mkern-7mu#2}}}%
}}
\newcommand\rawsat{\ThisStyle{\kern.5\LMpt
\scaleto{\rawsatimg}{2\LMex}\kern.5\LMpt}}
\savestack\rawsatimg{%
\begin{tikzpicture}
\coordinate (A) at (-.7,-.6);
\coordinate (B) at (-.2,-.6);
\coordinate (C) at (+.2,+.6);
\coordinate (D) at (+.7,+.6);
\draw [black, line width=3pt] (A)--(B)--(C)--(D);
\end{tikzpicture}%
}
\begin{document}
$x=\sat_{-5}^{10} u(t)$
$\scriptstyle x=\sat_{-5}^{10} u(t)$
$\scriptscriptstyle x= \sat_{-5}^{10} u(t)$
\end{document}
A solution with tikz
.
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{xparse}
\makeatletter
\newif\ifsat@limits
\sbox\z@{$\sum$}
\sbox\@ne{$\displaystyle\sum$}
\tikzset{sat/.search also=/tikz}
\def\satset#1{\tikzset{sat/.cd,#1}}
\satset{
height/.store in=\sat@ht,
is limits/.is if=sat@limits,
nodisplay/.code={
\@tempdima=\dimexpr\dp\z@+\ht\z@
\pgfkeysalso{height=\@tempdima}
},
display/.code={
\@tempdimb=\dimexpr\dp\@ne+\ht\@ne
\pgfkeysalso{height=\@tempdimb}
},
nolimits/.style={is limits=false},
limits/.style={is limits=true},
}
\NewDocumentCommand {\satpic} { O{} m m m } {
\begin{tikzpicture}[baseline=0.5\dp\[email protected]\ht\z@, x=1ex, y=1ex, sat/.cd, nolimits, nodisplay, #1]
\coordinate (A) at (-\sat@ht/2,-\sat@ht/2);
\coordinate (B) at (-\sat@ht/7,-\sat@ht/2);
\coordinate (C) at (\sat@ht/7,\sat@ht/2);
\coordinate (D) at (\sat@ht/2,\sat@ht/2);
\draw [black, line width=.6pt, #2] (A)--(B)--(C)--(D);
\ifsat@limits
\IfValueT {#3} {
\node [below=1.3pt, inner sep=0pt] at (current bounding box.south) {$\scriptstyle #3$};
}
\IfValueT {#4} {
\node [above=1.3pt, inner sep=0pt] at (current bounding box.north) {$\scriptstyle #4$};
}
\else
\IfValueT {#3} {
\node [anchor=west, inner sep=0pt, xshift=.3ex] at (B) {$\scriptstyle #3$};
}
\IfValueT {#4} {
\node [anchor=east, inner sep=0pt, xshift=-.3ex] at (C) {$\scriptstyle #4$};
}
\fi
\end{tikzpicture}
}
\makeatother
\NewDocumentCommand {\sat} { E{!@_^}{{}{}} } {
\mathchoice
{\satpic[display, limits, #1]{#2}{#3}{#4}}
{\satpic[nodisplay, nolimits, #1]{#2}{#3}{#4}}
{\satpic[nodisplay, nolimits, #1]{#2}{#3}{#4}}
{\satpic[nodisplay, nolimits, #1]{#2}{#3}{#4}}
}
\begin{document}
Saturation A: $\sat_{-5}^{10}$ and $\sat!{limits}_{123}^{456}$
Saturation B:
\[
\sat@{red}_{10}^{200} \sat@{line width=1pt}_{40000}
\]
\end{document}