LaTeX Box 中的垂直间距问题

LaTeX Box 中的垂直间距问题

我目前正在用 LaTeX 处理一份文档,在自定义框环境中遇到了垂直间距问题。我正在使用 tcolorbox 包为某些文本部分创建自定义框。但是,我注意到框中文本上方和下方的垂直间距高度不一样(附图中的红色和蓝色箭头表示)。在此处输入图片描述

这是我用于自定义框环境的代码片段:

\documentclass[11pt,a4paper]{memoir}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage{mleftright}
\usepackage{fontawesome}
\usepackage{amsmath, amssymb}
\usepackage{multicol}
\usepackage{microtype}
\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{lipsum}


\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{many}


\tcbset{
    enhanced,
    breakable,
    arc=0pt,
    colframe=black,
    boxsep=0pt,
    boxrule=1pt,
    colback=white,
    left=\dimexpr\tcboxedtitleheight/2\relax,
    right=0pt,
    top=0pt,
    bottom=0pt,
    attach boxed title to top left={
        xshift=\dimexpr\tcboxedtitleheight/2\relax,
        yshift=-\tcboxedtitleheight/2,
    },
    top=0mm,
    boxed title style={
        arc=0pt,
        colframe=black,
        colback=black,
        coltitle=white,
        boxrule=1pt,
        right=1.5pt,
        left=1.5pt,
        top=1.5pt,
        bottom=1.5pt,
    },
}

\newenvironment{mybox}[1]{%
    \begin{tcolorbox}[title={\bfseries#1}, before upper={\vspace{\dimexpr\tcboxedtitleheight}}, after upper={\vspace{\dimexpr\tcboxedtitleheight/2}}, before={\vspace{0pt}}, after={\vspace{0pt}}]%
    }{
    \end{tcolorbox}
}

\begin{document}

\chapter{Complex Numbers}

\section{Section}
\begin{mybox}{Pro\smash{p}erties of the modulus function}
The \textbf{modulus} of a complex number \(z = a + b\mathrm{i}\) is defined as \(\displaystyle{\vert z\vert} = {\sqrt{{a}^{2}+{b}^{2}}}\).
\end{mybox}
\end{document}

问题似乎源于某些字符的上升部以及下降部,例如单词“complex”中的上升部和平方根符号等函数。我尝试使用 \smash 命令,但解决方案不是最佳的,需要针对每个实例进行手动调整。

我也看过关于这个主题的类似帖子,但其他人提出的解决方案似乎都不适合我这种特定情况。

有人能建议一个更优雅、更有效的解决方案来确保我的自定义框环境中的垂直间距一致吗?任何帮助都将不胜感激。谢谢。

编辑:下面是另一段代码,显示了顶部和底部边距的差异会根据第一行的内容而变化。它看起来相当丑陋,我只想要一些更尊重垂直对称的东西(请参阅下面的评论)

\documentclass[11pt,a4paper]{memoir}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage{mleftright}
\usepackage{fontawesome}
\usepackage{amsmath, amssymb}
\usepackage{multicol}
\usepackage{microtype}
\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{lipsum}


\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{many}

\tcbset{
    enhanced,
    breakable,
    arc=0pt,
    colframe=black,
    boxsep=0pt,
    boxrule=1pt,
    colback=white,
    left=0pt,
    right=0pt,
    top=0pt,
    bottom=0pt,
    attach boxed title to top left={
        xshift=\dimexpr\tcboxedtitleheight/2\relax,
        yshift=-\tcboxedtitleheight/2,
    },
    top=0mm,
    boxed title style={
        arc=0pt,
        colframe=black,
        colback=black,
        coltitle=white,
        boxrule=1pt,
        right=1.5pt,
        left=1.5pt,
        top=1.5pt,
        bottom=1.5pt,
    },
}


\newenvironment{mybox}[1]{%
    \begin{tcolorbox}[title={\bfseries#1}, before upper={\vspace{\dimexpr\tcboxedtitleheight}}]%
    }{
    \end{tcolorbox}
}

\usepackage{lipsum}
\begin{document}

\chapter{Complex Numbers}

\section{Section}
\begin{mybox}{Pro\smash{p}erties of the modulus function}
The \textbf{modulus} of a complex number \(z = a + b\mathrm{i}\) is defined as \(\displaystyle{\vert z\vert} = {\sqrt{{a}^{2}+{b}^{2}}}\).
\end{mybox}
\begin{mybox}{Pro\smash{p}erties of the modulus function}
The \textbf{modulus} of a complex number \(z = a + b\mathrm{i}\) is defined as \(\displaystyle{\vert z\vert} = {\sqrt{{a}^{2}+{b}^{2}}}\). \lipsum[1] $\displaystyle{\frac{1}{x}}$
\end{mybox}
\begin{mybox}{Pro\smash{p}erties of the modulus function}
The \textbf{modulus} of a complex number
\end{mybox}
\begin{mybox}{Pro\smash{p}erties of the modulus function}
The \textbf{modulus} of a complex number $\dfrac{1}{x}$
\end{mybox}
\end{document}

相关内容