找到边界框的中间位置以制作自定义符号

找到边界框的中间位置以制作自定义符号

我想画一些有趣的积分符号,即“快乐”积分

快乐积分

但我在以尊重文本大小和显示样式的方式执行此操作时遇到了问题。我使用的是 egregs 的方法回答这里。

在我的“integrale.tex”中我有

\makeatletter
\newlength{\mybox}
\DeclareRobustCommand\inttest{\vphantom{\int}\mathpalette\aw@qc\relax}
\newcommand{\aw@qc}[2]{%
    \sbox\mybox{$#1 \int$}%
    \dimen1 = \dimexpr\wd\mybox\relax %"Width" - Width of the bounding box
    \dimen2 = \dimexpr\ht\mybox\relax %"Height" - distance between baseline and upper box side
    \dimen3 = \dimexpr\dp\mybox\relax %"Depth" - Abstand zwischen baseline lower box side
    \dimen4 = 0.5\dimexpr\ht\z@-\dp\z@\relax %y_coordinate of center of bounding box (Average of Height and Depth)
    \fbox{$#1 \int$}\put(-\dimen1,\dimen4){\relativearms}
}
\makeatother

\newcommand{\test}{\relativearms \text{\ \ \ } \text{\ \ \ } \inttest abc}
\newcommand{\relativearms}{\put(0em,0em){\line(-1,2.2){0.15em}}\put(0em,0em){\line(1,2.2){0.15em}}}

为了测试结果我使用“testing.tex”

\documentclass[a4paper,12pt]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage{pict2e} % To get around errorof \line :"LaTeX Warning: \oval, \circle, or \line size unavailable"
\usepackage{fbox}
\input{integrale.tex}
\begin{document}
%Testing

\begin{footnotesize}
    Footnotesize:
    $\textstyle \test$
    $\displaystyle \test$
    $ \scriptstyle \test$
    $ \scriptscriptstyle \test$
\end{footnotesize}\\ \ \\

\begin{small}
    small:
    $\textstyle \test$
    $\displaystyle \test$
    $ \scriptstyle \test$
    $ \scriptscriptstyle \test$
\end{small}\\ \ \\

\begin{normalsize}
    normalsize:
    $\textstyle \test$
    $\displaystyle \test$
    $ \scriptstyle \test$
    $ \scriptscriptstyle \test$
\end{normalsize}\\ \ \\

\begin{large}
    large:
    $\textstyle \test$
    $\displaystyle \test$
    $ \scriptstyle \test$
    $ \scriptscriptstyle \test$
\end{large}\\ \ \\

\begin{Large}
    Large:
    $\textstyle \test$
    $\displaystyle \test$
    $ \scriptstyle \test$
    $ \scriptscriptstyle \test$
\end{Large}\\ \ \\

\end{document}

这将产生输出 输出

  1. (最重要的):
    正如您所看到的,只有在显示样式中,我的手臂才不是从盒子的中间开始,我不知道为什么以及如何解决这个问题。
  2. 臂不按比例缩放到积分符号。“大”行的积分臂太长,无法成比例。为什么积分符号似乎不会随着文本尺寸的增大而变大?(对于大版本,臂接触到头部。)

答案1

以下解决方案是可能只有那么长,因为我完全是 tikz 新手。不要因为长度而退缩。解决方案的关键部分是:

\newcommand{\happyint}{\mathpalette\happyint@\relax}
\newcommand{\happyint@}[2]{
    %#1 is mathstyle e.g \displaystyle or \textstyle #2 not used but needed because of mathpalette
    \tikz[baseline=(box.base), inner sep=0pt,outer sep=0pt]{
        \node[anchor=center] (box) at (0,0) {$#1\int$};
        \computeCoords{box}
        %\drawbox@
        \happyarms@{#1}
    }
}

另请参阅输出在这篇文章的底部

我最终找到了解决办法。本质上我的方法没有错,我仍然不知道为什么它不起作用。现在我只使用 tikz 来绘制手臂。我仍然需要在显示模式下调整我正在绘制的内容,但这是因为积分的纵横比本质上发生了变化,因此如果不进行调整,它们会太长且太靠右(但现在在每个数学模式下调整总是相同的)

本质上我正在使用 tikz 来获得一个围绕正常积分的框(取消注释 \drawbox 以查看该框)计算一些向量并根据框坐标绘制我的手臂。

我也找到了一种方法来获得与正常积分符号完全相同的边界框即使您决定在 tikz 框外绘图。请参阅integrale_min.tex 末尾的注释(由于某种原因,它增加了大量的编译时间,但对于此示例而言,这是不需要的):

\usepackage{mathtools} %for \mathmakebox if needed
\usepackage{tikz}
\makeatletter
\usetikzlibrary{calc}


\newcounter{inDispStyle}
\newcommand{\computeCoords}[1]{
    \coordinate (centerToNorth) at ($ (#1.north) - (#1.center) $);
    \coordinate (centerToEast) at ($ (#1.east) - (#1.center) $);
    \coordinate (centerToSouth) at ($ (#1.south) - (#1.center) $);
    \coordinate (centerToWest) at ($ (#1.west) - (#1.center) $);
    \coordinate (center) at (#1.center);
    \coordinate (north) at (#1.north);
    \coordinate (east) at (#1.east);
    \coordinate (south) at (#1.south);
    \coordinate (west) at (#1.west);
    \coordinate (northwest) at (#1.north west);
    \coordinate (northeast) at (#1.north east);
    \coordinate (southeast) at (#1.south east);
    \coordinate (southwest) at (#1.south west);

}

\newcommand{\drawbox@}{
    \draw (northwest) -- (northeast) ;
    \draw (northeast) -- (southeast) ;
    \draw (southeast) -- (southwest) ;
    \draw (southwest) -- (northwest) ;
}

\newcommand{\happyarms@}[1]{
    %#1 should be mathstyle, passed through from mathpalette
    \ifx#1\displaystyle
        \setcounter{inDispStyle}{1}
    \else
        \setcounter{inDispStyle}{0}
    \fi
    %
    \coordinate (rightArmBegin) at ($(center)+0.1*(centerToNorth)+0.1*\theinDispStyle*(centerToEast)$);
    \coordinate (leftArmBegin) at ($(center)+0.1*(centerToNorth)+0.1*\theinDispStyle*(centerToEast)$);
    \coordinate (rightArmEndFull) at ($(centerToEast)+0.55*(centerToNorth)$);
    \coordinate (leftArmEndFull) at ($(centerToWest)+0.85*(centerToNorth)$);
    \coordinate (rightArmEnd) at ($(rightArmBegin)!0.75!(rightArmEndFull)$);
    \coordinate (leftArmEnd) at ($(leftArmBegin)!0.6!(leftArmEndFull)$);
    \draw (rightArmBegin) -- (rightArmEnd);
    \draw (leftArmBegin) -- (leftArmEnd);
}

\newcommand{\happyint}{\mathpalette\happyint@\relax}
\newcommand{\happyint@}[2]{
    %#1 is mathstyle e.g \displaystyle or \textstyle #2 not used but needed because of mathpalette
    \tikz[baseline=(box.base), inner sep=0pt,outer sep=0pt]{
        \node[anchor=center] (box) at (0,0) {$#1\int$};
        \computeCoords{box}
        %\drawbox@
        \happyarms@{#1}
    }
}
%If you want the result to have exactly the same bounding box as a regular integral regardless of what you decide to draw with tikz you can use: 
%\newcommand{\happyintvariant}{\smash{\mathmakebox[0pt][l]{\mathpalette\happyint@\relax}}\phantom{\int}}
\makeatother
\newcommand{\test}{\happyint}
\newcommand{\vergleich}{\int}

测试文件在每个尺寸的每个数学模式中在正常积分旁边绘制快乐积分。添加了fboxes,因此我们可以看到这种情况下边界框与正常的相同(即使不使用注释行)。

您可以将文本大小更改\documentclass[a4paper,12pt]{article}为您喜欢的任何大小。

我的 testing.tex 包含:

\documentclass[a4paper,12pt]{article}
\input{integrale.tex}
\begin{document}
%Testing
\makeatletter
\begin{footnotesize}
    Footnotesize:
    \fbox{\m@th $\textstyle \test $} \fbox{\m@th $\textstyle \vergleich $}
    \fbox{\m@th $\displaystyle \test $} \fbox{\m@th $\displaystyle \vergleich $}
    \fbox{\m@th $\scriptstyle \test $} \fbox{\m@th $\scriptstyle \vergleich $}
    \fbox{\m@th $\scriptscriptstyle \test $} \fbox{\m@th $\scriptscriptstyle \vergleich $}
\end{footnotesize}\\ \ \\

\begin{small}
    small:
    \fbox{\m@th $\textstyle \test $} \fbox{\m@th $\textstyle \vergleich $}
    \fbox{\m@th $\displaystyle \test $} \fbox{\m@th $\displaystyle \vergleich $}
    \fbox{\m@th $\scriptstyle \test $} \fbox{\m@th $\scriptstyle \vergleich $}
    \fbox{\m@th $\scriptscriptstyle \test $} \fbox{\m@th $\scriptscriptstyle \vergleich $}
\end{small}\\ \ \\

\begin{normalsize}
    normalsize:
    \fbox{\m@th $\textstyle \test $} \fbox{\m@th $\textstyle \vergleich $}
    \fbox{\m@th $\displaystyle \test $} \fbox{\m@th $\displaystyle \vergleich $}
    \fbox{\m@th $\scriptstyle \test $} \fbox{\m@th $\scriptstyle \vergleich $}
    \fbox{\m@th $\scriptscriptstyle \test $} \fbox{\m@th $\scriptscriptstyle \vergleich $}
\end{normalsize}\\ \ \\

\begin{large}
    large:
    \fbox{\m@th $\textstyle \test $} \fbox{\m@th $\textstyle \vergleich $}
    \fbox{\m@th $\displaystyle \test $} \fbox{\m@th $\displaystyle \vergleich $}
    \fbox{\m@th $\scriptstyle \test $} \fbox{\m@th $\scriptstyle \vergleich $}
    \fbox{\m@th $\scriptscriptstyle \test $} \fbox{\m@th $\scriptscriptstyle \vergleich $}
\end{large}\\ \ \\

\begin{Large}
    Large:
    \fbox{\m@th $\textstyle \test $} \fbox{\m@th $\textstyle \vergleich $}
    \fbox{\m@th $\displaystyle \test $} \fbox{\m@th $\displaystyle \vergleich $}
    \fbox{\m@th $\scriptstyle \test $} \fbox{\m@th $\scriptstyle \vergleich $}
    \fbox{\m@th $\scriptscriptstyle \test $} \fbox{\m@th $\scriptscriptstyle \vergleich $}
\end{Large}\\ \ \\
\makeatother
\end{document}

输出结果如下: 输出

相关内容