A common math notation for restricting, to a subset A of its domain, a function f from a set X into a set Y is given by a vertical bar as shown in the first section of output shown below. This is a method by Enrico Gregorio ("egreg") https://tex.stackexchange.com/a/22255/13492.
I want to change the vertical bar to an \upharpoonright. Using the method by Heiko Oberdiek https://tex.stackexchange.com/a/67233/13492, I get the result shown in the second section of output below.
In this method with \upharpoonright, however, the \upharpoonright has its top too low and its bottom also too low. How can that be fixed, so that the vertical position is essentially the same as that used with the vertical bar?
\documentclass{article}
\usepackage{amsfonts,amssymb}
\usepackage[left=1.25in,right=1.25in]{geometry}
\usepackage{scalerel}
% Method of Enrico Gregorio ("egreg")
% https://tex.stackexchange.com/a/22255/13492:
\newcommand\restrict[2]{% make the whole thing an ordinary symbol
\left.\kern-\nulldelimiterspace % automatically resize the bar with \right
#1% the function
\littletaller % pretend it's a little taller at normal size
\right|_{#2}%
}
\newcommand{\littletaller}{\mathchoice{\vphantom{\big|}}{}{}{}}
% Method of Heiko Oberdiek
% https://tex.stackexchange.com/a/67233/13492
\makeatletter
\newcommand{\scaleddelims}[3]{%
\ensuremath{%
\mathpalette{@scaleddelims{#1}{#2}}{#3}%
}%
}
\newcommand{@scaleddelims}[4]{%
% #1: left delimiter
% #2: right delimiter
% #3: \displaystyle, \textstyle, ...
% #4: inner formula
\begingroup
#3%
\sbox0{$\m@th#3\vphantom{A}#4$}%
\setbox2\vbox{\hbox{$\m@th#3#1$}\kern\z@}%
\setbox4\vbox{\hbox{$\m@th#3#2$}\kern\z@}%
\setbox6\hbox{$#3\vcenter{}$}%
\ifx\downharpoonleft#1\relax
\let\DelimLeft=L%
\else\ifx\upharpoonleft#1%
\let\DelimLeft=L%
\else\ifx\downharpoonright#1%
\let\DelimLeft=R%
\else\ifx\upharpoonright#1%
\let\DelimLeft=R%
\fi\fi\fi\fi
\ifx\downharpoonleft#2\relax
\let\DelimRight=L%
\else\ifx\upharpoonleft#2\relax
\let\DelimRight=L%
\else\ifx\downharpoonright#2\relax
\let\DelimRight=R%
\else\ifx\upharpoonright#2\relax
\let\DelimRight=R%
\fi\fi\fi\fi
\ifx\DelimLeft L%
\wd2=.6\wd2
\fi
\ifx\DelimRight L%
\wd4=.6\wd4
\fi
\ifx\DelimLeft R%
\sbox2{\kern-.4\wd2\box2}%
\fi
\ifx\DelimRight R%
\sbox4{\kern-.4\wd4\box4}%
\fi
\dimen0=\ht0 %
\advance\dimen0 by -\ht6 %
\dimen2=\dp0 %
\advance\dimen2 by \ht6 %
\ifdim\dimen2>\dimen0 %
\dimen0=\dimen2 %
\else
\dimen0=\dimen0 %
\fi
\dimen2=\ht6 %
\advance\dimen2 by -\dimen0 %
\dimen0=2\dimen0 %
\def\DelimCorr{%
\mskip.5\thinmuskip
\nonscript\mskip.5\thinmuskip
}%
\mathopen{%
\ifx\DelimLeft R\DelimCorr\fi
\raisebox{\dimen2}{\resizebox{!}{\dimen0}{\box2}}%
\ifx\DelimLeft L\DelimCorr\fi
}%
\begingroup
#3#4%
\endgroup
\mathclose{%
\ifx\DelimRight R\DelimCorr\fi
\raisebox{\dimen2}{\resizebox{!}{\dimen0}{\box4}}%
\ifx\DelimRight L\DelimCorr\fi
}%
\endgroup
}\makeatother
%
\newcommand{\rest}[2]{#1\scaleddelims{\kern-0.5\nulldelimiterspace\upharpoonright}{\vphantom{.}}{_{#2}}}
\begin{document}
\verb!\restrict! (with \verb!|!):
Map $\restrict{f}{E} \colon E \to Y, \quad \restrict{g}{E} \colon E \to Y, \quad
\restrict{\Phi}{E} \colon E \to Y, \quad \restrict{f}{A, B} \colon A \to B, \quad \restrict{f}{(A, B)} \colon A \to B$
\bigskip
\verb!\rest! (with \verb!\upharpoonright!):
Map $\rest{f}{E} \colon E \to Y, \quad \rest{g}{E} \colon E \to Y, \quad
\rest{\Phi}{E} \colon E \to Y, \quad \rest{f}{A, B} \colon A \to B, \quad \rest{f}{(A, B)} \colon A \to B$
\end{document}
I am aware that the amssymb package defines \restriction in effect as \mathrel{\upharpoonright} (compare https://tex.stackexchange.com/a/565926/13492). However, the horizontal spacing is then too great, and in any case the height does not seem to change appropriately.



\resizeboxfor this, since for taller expressions it looks horrible. Please see the other solution that I am typing up right now, it looks nicer and automatically has the same depth (subscript location) as what egreg made. – Willie Wong Jul 28 '23 at 01:26