![测量 \int 的间距](https://linux22.com/image/377067/%E6%B5%8B%E9%87%8F%20%5Cint%20%E7%9A%84%E9%97%B4%E8%B7%9D.png)
我希望能够测量一个 占用的空间,\int
并将该长度存储在 中\newlength{}
。这并不像
\settowidth{\@IntWidth}{\widthof{$\int$}}
在下面的 MWE 中,我尝试应用\kern
测量间距的负数,并用红色重新绘制符号。如果测量正确,红色符号应该正好位于黑色符号的上方,但我得到了:
笔记:
- 这仅解决给定上限和下限的情况,因此处理其他选项的代码被注释掉。
代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{xparse}
\newcommand{\dx}{\mathrm{d}x}%
\makeatletter
\let\OldIntOp\int
\newlength{\@IntWidth}
%% https://tex.stackexchange.com/a/345383/4301
\RenewDocumentCommand\int{e{_^}}{% underscore ALWAYS before ^
%\IfNoValueTF{#2}{% No upper limit
% \IfNoValueTF{#1}{% No lower and upper limit
% \OldIntOp
% }{% Only lower limit
% \OldIntOp_{#1}
% }
%}{
% \IfNoValueTF{#1}{% No lower, but do have upper limit
% \OldIntOp^{#2}
% }{% Both lower and upper limit
\OldIntOp
\settowidth{\@IntWidth}{\widthof{$\OldIntOp$}}% ????? How compute this ??????
\kern-\@IntWidth
{\color{red} \OldIntOp_{#1}^{#2}}
% }
%}%
}\makeatother
\begin{document}
\noindent
In inline math $\OldIntOp_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\OldIntOp^b_a y\dx
\textstyle\OldIntOp_a^b y\dx
\scriptstyle\OldIntOp_a^b y\dx
\scriptscriptstyle\OldIntOp_a^b y\dx
&
\end{flalign*}
In inline math $\int_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\int^b_a y\dx
\textstyle\int_a^b y\dx
\scriptstyle\int_a^b y\dx
\scriptscriptstyle\int_a^b y\dx
&
\end{flalign*}
\end{document}
答案1
不确定您想要得到什么。无论如何,可以使用来计算宽度\mathpalette
(还有一个\!
要添加的,很可能是一些\mathop
诡计)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{xparse}
\newcommand{\dx}{\mathrm{d}x}%
\makeatletter
\let\OldIntOp\int
\newcommand\kernint{\mathpalette\@kernint\relax}
\newcommand\@kernint[2]{%
\!\sbox0{$\m@th#1\OldIntOp$}\kern-\wd0
}
%% https://tex.stackexchange.com/a/345383/4301
\RenewDocumentCommand\int{e{_^}}{% underscore ALWAYS before ^
%\IfNoValueTF{#2}{% No upper limit
% \IfNoValueTF{#1}{% No lower and upper limit
% \OldIntOp
% }{% Only lower limit
% \OldIntOp_{#1}
% }
%}{
% \IfNoValueTF{#1}{% No lower, but do have upper limit
% \OldIntOp^{#2}
% }{% Both lower and upper limit
\OldIntOp\kernint
{\color{red} \OldIntOp_{#1}^{#2}}
% }
%}%
}
\makeatother
\begin{document}
\noindent
In inline math $\OldIntOp_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\OldIntOp^b_a y\dx
\textstyle\OldIntOp_a^b y\dx
\scriptstyle\OldIntOp_a^b y\dx
\scriptscriptstyle\OldIntOp_a^b y\dx
&
\end{flalign*}
In inline math $\int_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\int^b_a y\dx
\textstyle\int_a^b y\dx
\scriptstyle\int_a^b y\dx
\scriptscriptstyle\int_a^b y\dx
&
\end{flalign*}
\end{document}
\makeatother
\begin{document}
\noindent
In inline math $\OldIntOp_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\OldIntOp^b_a y\dx
\textstyle\OldIntOp_a^b y\dx
\scriptstyle\OldIntOp_a^b y\dx
\scriptscriptstyle\OldIntOp_a^b y\dx
&
\end{flalign*}
In inline math $\int_a^b y\dx$, and in display math it is:
\begin{flalign*}
&\displaystyle\int^b_a y\dx
\textstyle\int_a^b y\dx
\scriptstyle\int_a^b y\dx
\scriptscriptstyle\int_a^b y\dx
&
\end{flalign*}
\end{document}
答案2
如果您对数学框的实际测量感兴趣,以下示例测量数学公式并将其尺寸写入文件。这解决了/在所有数学样式中设置公式.aux
的限制,但 TeX 稍后会决定使用哪个版本(感谢和朋友)。\mathpalette
\mathchoice
\over
\documentclass{article}
\usepackage{color}
\newcommand*{\diff}{\mathop{}\!\mathrm{d}}
\makeatletter
% Create new label type for the .aux file that takes
% the measurements of the math box.
% LaTeX takes care of re-run warnings Because of the usage of \@newl@bel.
\usepackage{auxhook}
\AddLineBeginAux{%
\string\providecommand\string\New@MeasureMathBox[2]{}%
}
\newcommand*{\New@MeasureMathBox}{%
\@newl@bel{mmb}%
}
% Counter for identifying a math box
\newcounter{MeasureMathBox}
\renewcommand*{\theMeasureMathBox}{\the\value{MeasureMathBox}}
% Register that contain the measurement result
\newdimen\LastMathBoxWidth
\newdimen\LastMathBoxHeight
\newdimen\LastMathBoxDepth
% Measure the math box and write the label
% with the measurements to the .aux file
% in all styles via \mathpalette.
% When TeX later decides, which box it uses,
% only one write command of the correct
% math style remains.
\newcommand*{\MeasureMathBox}{%
% #1: math formula to be measured
% New identifier for the math box.
\stepcounter{MeasureMathBox}%
% Read the label if available.
\@ifundefined{mmb@\theMeasureMathBox}{%
\LastMathBoxWidth\z@
\LastMathBoxHeight\z@
\LastMathBoxDepth\z@
}{%
\expandafter\expandafter\expandafter
\Extract@MeasureMathBox
\csname mmb@\theMeasureMathBox\endcsname\@nil
}%
\mathpalette\@MeasureMathBox
}
\def\Extract@MeasureMathBox#1,#2,#3\@nil{%
\LastMathBoxWidth#1\relax
\LastMathBoxHeight#2\relax
\LastMathBoxDepth#3\relax
}
\newcommand*{\@MeasureMathBox}[2]{%
% #1: math style
% #2: math formula to be measured
% Do the actual measurement.
\setbox0=\hbox{$#1#2\m@th$}%
% Write the label to the .aux file.
\protected@write\@auxout{}{%
\string\New@MeasureMathBox
{\theMeasureMathBox}%
{\the\wd0,\the\ht0,\the\dp0}%
}%
}
\makeatother
\begin{document}
\def\Test{
\int
\MeasureMathBox{\int}
\kern-\LastMathBoxWidth
\! % negate horizontal spacing between two operators
\begingroup\color{red} \int_a^b \endgroup y\diff x
}
\[ \Test \]
\[ \textstyle \Test \]
\[ \scriptstyle \Test \]
\[ \scriptscriptstyle \Test \]
\end{document}
第一次编译运行后,该.aux
文件包含:
\relax
\providecommand\New@MeasureMathBox[2]{}
\New@MeasureMathBox{1}{10.00002pt,13.61122pt,8.61124pt}
\New@MeasureMathBox{2}{6.66667pt,8.0556pt,3.05562pt}
\New@MeasureMathBox{3}{6.66667pt,7.3056pt,3.80562pt}
\New@MeasureMathBox{4}{6.66667pt,6.8056pt,4.30562pt}
这在第二次编译运行中使用:
如果你只对插入数学公式的负空间感兴趣,下面的操作可以在一次编译运行中完成(类似于 egreg 的回答,但更为通用):
\documentclass{article}
\usepackage{color}
\newcommand*{\diff}{\mathop{}\!\mathrm{d}}
\makeatletter
\newcommand*{\NegMathWidth}{%
% #1: math formula to be measured
\mathpalette{\@NegMathWidth}%
}
\newcommand*{\@NegMathWidth}[2]{%
% #1: math style
% #2: math formula to be measured
\settowidth\dimen@{$#1#2\m@th$}%
\kern-\dimen@
}
\makeatother
\begin{document}
\def\Test{
\int
\NegMathWidth{\int}
\! % negate horizontal spacing between two operators
\begingroup\color{red} \int_a^b \endgroup y\diff x
}
\[ \Test \]
\[ \textstyle \Test \]
\[ \scriptstyle \Test \]
\[ \scriptscriptstyle \Test \]
\end{document}
结果和上面一样。