控制左上标间距的低级命令

控制左上标间距的低级命令

这个问题导致了一个新的方案的出现:左索引

一个众所周知的问题是,似乎没有自动的方法可以为左索引(尤其是左上标)获得良好的间距。最标准的命令选择似乎是\prescript来自mathtools,但上标与其他选项一样存在同样的问题:

在此处输入图片描述

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\[
    \prescript{a}{b}{f},
    \qquad
    \prescript{a}{b}{\int}
\]

\end{document}

我想要的是针对此问题的低级、手动解决方案:一个命令

\manualprescript{<height>}{<superscript indentation>}{<subscript indentation>}
        {<superscript>}{<subscript>}{<symbol>}

我可以手动指定索引的高度和缩进。这可以使用 TeX 尺寸(例如.3em)或作为要插入\vphantom和的标记\hphantom。这两种解决方案我都接受(或者两种都接受,如果你有时间的话)。我自己可能想出一些(非常)糟糕的解决方案,使用方框和\phantom,这就是为什么我在这里问这个问题,以便获得正确的解决方案。(如果可能的话,我更喜欢使用 LaTeX3 语法的解决方案,因为这通常更具可读性并且面向未来)。

答案1

您可以自动完成此操作。

\documentclass{article}
\usepackage{amsmath,mathtools,xparse}

\makeatletter
\NewDocumentCommand{\preint}{e{^_}}{\mathpalette\pre@int{{#1}{#2}}\!\int}
\newcommand{\pre@int}[2]{\pre@@int#1#2}
\newcommand{\pre@@int}[3]{%
  \sbox0{$#1\int\sb{xxxxxx}$}%
  \sbox2{$#1{\int}\sb{xxxxxx}$}%
  \setlength{\dimen4}{\dimexpr\wd2-\wd0\relax}%
  % compute the spacing
  \settowidth{\dimen0}{$\m@th#1^{\IfValueT{#2}{#2}}$}%
  \addtolength{\dimen0}{-\dimen4}%
  \settowidth{\dimen2}{$\m@th#1_{\IfValueT{#3}{#3}}$}%
  % start the business
  \mathop{}\!%
  \ifdim\dimen0>\dimen2 \kern\dimen0\else\kern\dimen2\fi
  % print the scripts
  {\mbox{$#1\vphantom{\int}$}}
  ^{\mathmakebox[0pt][l]{\mathmakebox[\dimen4][r]{\IfValueT{#2}{#2}\mspace{-1mu}}}}
  _{\mathmakebox[0pt][r]{\IfValueT{#3}{#3}}}
 }
\makeatother

\begin{document}

\[
x\preint_a^b f(x)\,dx + \preint_a^{b+c+d} f(x)\,dx
\]
\begin{center}
$x\preint_a^b$
\end{center}

\end{document}

在此处输入图片描述

答案2

该解决方案现已作为套件提供:左索引

我尝试调整 egreg 的答案以适应一般情况。它不是专门为该\int特定符号构建的,因此不会产生非常完美的输出。好处是它在一般情况下提供了相对不错的结果。我提供了命令

\leftindex^{<left superscript>}_{<left subscript>} {<symbol>}

这将使左上标的缩进值与右下标的负缩进值相同。有时,这会产生错误的结果。因此,该命令需要两个额外的可选参数:

\leftindex[<slanting phantom>][<height phantom>]
    ^{<left superscript>}_{<left subscript>} {<symbol>}

这个将使用 来计算左上标缩进<slanting phantom>。如果提供,它将根据 来计算高度<height phantom>

还有另一个底层命令

\manualleftindex
    {<height phantom>}
    {<slanting phantom>}
    {<subscript>}
    {<superscript>}

其主要用于其他命令(或包)。

\documentclass{article}

\usepackage{expl3,mathtools,kpfonts}

\ExplSyntaxOn

\DeclareDocumentCommand\manualleftindex { mmmm }
{
    % #1 = height phantom
    % #2 = slanting phantom
    % #3 = left superscript
    % #4 = left subscript
    \mathpalette \__leftindex_auxiliary_mathpalette_command:nn { {#1}{#2}{#3}{#4} }
}

\ExplSyntaxOff

\DeclareDocumentCommand\leftindex { o o E{^_}{{}{}} m }
{
    % #1 = slanting phantom
    % #2 = height phantom
    % #3 = left superscript
    % #4 = left subscript
    % #5 = symbol
    \IfValueTF {#1}
    {
        \IfValueTF{#2}
        {
            \manualleftindex { #2 } { #1 } { #3 } { #4 }
        }
        {
            \manualleftindex { #1 } { #1 } { #3 } { #4 }
        }
    }
    {
        \manualleftindex { #5 } { #5 } { #3 } { #4 }
    }
    #5
}

\DeclareDocumentCommand\LeftIndex { o o E{^_}{{}{}} }
{
    % #1 = slanting phantom
    % #2 = height phantom
    % #3 = left superscript
    % #4 = left subscript
    \IfValueTF {#1}
    {
        \IfValueTF{#2}
        {
            \manualleftindex { #2 } { #1 } { #3 } { #4 }
        }
        {
            \manualleftindex { #1 } { #1 } { #3 } { #4 }
        }
    }
    {
        \manualleftindex { } { } { #3 } { #4 }
    }
}

\ExplSyntaxOn

\cs_new_protected:Npn\leftindex_kern_horizontal:n#1
{
    \kern #1 \relax
}

\cs_new_protected:Npn\leftindex_set_mathsurround_to_zero:
{
     % This is equivalent to "\m@th"
    \dim_set:Nn \mathsurround { 0pt }
}

\cs_new_protected:Npn\__leftindex_auxiliary_mathpalette_command:nn#1#2
{
    \__leftindex_auxiliary:nnnnn { #1 } #2
}

\dim_new:N\l__leftindex_phantom_height_dim

\box_new:N \l__leftindex_slanting_phantom_with_subscript_box
\dim_new:N \l__leftindex_slanting_phantom_with_subscript_dim

\box_new:N \l__leftindex_slanting_phantom_with_subscript_without_indentation_box
\dim_new:N \l__leftindex_slanting_phantom_with_subscript_without_indentation_dim

\dim_new:N \l__leftindex_indentation_of_slanting_phantom_subscript

\box_new:N \l__leftindex_superscript_temp_box
\dim_new:N \l__leftindex_width_of_superscript_dim

\box_new:N \l__leftindex_subscript_temp_box
\dim_new:N \l__leftindex_width_of_subscript_dim

\cs_new_protected:Npn\__leftindex_auxiliary:nnnnn#1#2#3#4#5
{
    \group_begin:
        \hbox_set:Nn \l__leftindex_slanting_phantom_with_subscript_box
            { $ #1 #3 \sb{xxxxxx} $ }
        \dim_set:Nn \l__leftindex_slanting_phantom_with_subscript_dim
            { \box_wd:N \l__leftindex_slanting_phantom_with_subscript_box }
        \hbox_set:Nn \l__leftindex_slanting_phantom_with_subscript_without_indentation_box
            { $ #1 \hbox:n {$ #1 #3 $} \sb{xxxxxx} $ }
        \dim_set:Nn \l__leftindex_slanting_phantom_with_subscript_without_indentation_dim
            { \box_wd:N \l__leftindex_slanting_phantom_with_subscript_without_indentation_box }
        \dim_set:Nn \l__leftindex_indentation_of_slanting_phantom_subscript
        {
            \l__leftindex_slanting_phantom_with_subscript_without_indentation_dim
            -
            \l__leftindex_slanting_phantom_with_subscript_dim
        }
        \tl_if_blank:nTF { #4 }
        {
            \dim_zero:N \l__leftindex_width_of_superscript_dim
        }
        {
            \hbox_set:Nn \l__leftindex_superscript_temp_box
            {
                $
                \leftindex_set_mathsurround_to_zero:
                #1 \sp { #4 }
                $
            }
            \dim_set:Nn \l__leftindex_width_of_superscript_dim
            {
                \box_wd:N \l__leftindex_superscript_temp_box
                -
                \l__leftindex_indentation_of_slanting_phantom_subscript
            }
        }
        \tl_if_blank:nTF { #5 }
        {
            \dim_zero:N \l__leftindex_width_of_subscript_dim
        }
        {
            \hbox_set:Nn \l__leftindex_subscript_temp_box
            {
                $
                \leftindex_set_mathsurround_to_zero:
                #1 \sb { #5 }
                $
            }
            \dim_set:Nn \l__leftindex_width_of_subscript_dim
            {
                \box_wd:N \l__leftindex_subscript_temp_box
            }
        }
        \mathop{}
        \leftindex_kern_horizontal:n
        {
            \dim_max:nn
                { \l__leftindex_width_of_superscript_dim }
                { \l__leftindex_width_of_subscript_dim }
        }
        \mathopen{ \vphantom { #2 } }
        \tl_if_blank:nF { #4 }
        {
            \sp {
                \mathmakebox[0pt][l]{
                    \mathmakebox[ \l__leftindex_indentation_of_slanting_phantom_subscript ][r]{ #4 }
                }
            }
        }
        \tl_if_blank:nF { #5 }
        {
            \sb {
                \mathmakebox[0pt][r]{ #5 }
            }
        }
    \group_end:
}

\ExplSyntaxOff

\begin{document}

\[
    x \leftindex^{a+b}_{c+d} {\int} f(x)\, dx
\]


\[
    \leftindex^{1}_{0} {f}^u_v
    \neq
    \leftindex^{pq}_{0} {H}^u_v
\]

\[
    \leftindex^{a}_{b} {\Gamma}^c_d
    \neq
    \leftindex[]^{a}_{b} {\Gamma}^c_d
\]


\end{document}

在此处输入图片描述

相关内容