需要帮助删除封闭的 NiceTabular 框中的空格

需要帮助删除封闭的 NiceTabular 框中的空格

问题描述

需要帮助来看看我遗漏了什么,下面的 NiceTabular 框中的文本上方有太多的间距。

梅威瑟:

% Note: The following code may need to be run twice
\documentclass{article}
\usepackage{nicematrix}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{tikz}
\usepackage{varwidth}

\definecolor{BrickRed}{RGB}{144,44,30} 

\makeatletter
\newcommand\niceMfbox[4][-2mm]{%
\vspace{#1}%
\begin{center}%     % The package nicematrix∗. Pgs 39-40
\begin{NiceTabular}{@{\hspace{20pt}}V{#2}@{\hspace{20pt}}}\bfseries\large%
\hspace*{0.5em} #3\hspace*{0.5em} \\%
\vspace{-8mm}\RowStyle[cell-space-limits=10pt]{}%   % space above and below text
#4%
\CodeAfter%
\tikz \draw [line width=1pt,rounded corners,BrickRed!75!black] (1-1) -| (last-|2) -- (last-|1) |- (1-1) ;%
\end{NiceTabular}%
\end{center} \par\vspace{2mm}%
}
\makeatother

% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\begin{document}

\begin{niceMfbox}[+1mm]{10.8cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}{\textrm{%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war. -NY Times”%
\vspace{-2mm}%
}}%
\end{niceMfbox}

\end{document}

期望的输出/解决方案

我想缩小框标题之间的间距初学者指南...以及框内的文字“站立式划桨冲浪...”。可能看起来不多,但我一直苦恼于无法缩小间距。我尝试了很多排列和卷积,但都无济于事\vspace(-somenumber),足以让我知道我超出了我的能力范围! NiceTabular 框但间距太大。 任何想法都值得赞赏...

更新:

第一的:感谢两位作者对我的帖子提供的解决方案!

第二:我有很多解决方案来改进我的代码,而不是一个,而是两个。这不是[Solomon's]2我很艰难的选择,因为我有一个我的故事情节中有几十个地方我用过我的niceMfbox代码。所以我将对这两个解决方案分别进行测试我的故事情节中有几十个地方以确定哪种解决方案总体表现最佳。然后我将投票确认两种解决方案之一为“最好的'解决方案。然而,我的选择可能不是'最好的' 适用于所有 LaTeX 应用程序,但这里如此详细地提供每个解决方案可以帮助其他人将来使用他们特定的 LaTeX 应用程序。

使用 SebGlav 的 tcolorbox 代码进行更新:

我已经调整了 SebGlav 的 tcolorbox LaTeX 代码以满足我的特定需求,但毫无疑问,它是 SebGlav 所有答案中迄今为止最好的解决方案。我对 SebGlav 的所有工作表示敬意,这使它成为我能想象到的最佳解决方案。谢谢 SebGlav。现在,我将在我的项目中最终使用 MWE:

% Note: The following code may need to be run twice
\documentclass{article}
\usepackage{nicematrix}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage[skins]{tcolorbox}
\usepackage{tikz}
\usepackage{varwidth}

\definecolor{BrickRed}{RGB}{144,44,30} 

%________________________ Define \mytcolorbox ________________________
\newtcolorbox[auto counter,number within=section]{mytcolorbox}[2]{
    frame style={draw=BrickRed!75!black},   % ,thick
    halign=left,
    valign=center,
    center,
    fonttitle=\large\bfseries,
    drop fuzzy shadow,
    boxrule=0.75pt,
    width=#1,
    colframe=BrickRed!75!black,
    colback=white,
    coltitle=BrickRed!75!black,
    colbacktitle=white,
    enhanced,
    boxed title style={boxrule=-1pt,left=-2pt,right=-2pt},
    attach boxed title to top center={yshift=-9pt},
    title={#2}\vspace*{+2mm}}

%_____________________ Define \reallywidetilde _____________________
% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\begin{document}

\begin{mytcolorbox}{12.3cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}\textrm{%
\newline%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war." -NY Times%
\vspace*{+2mm}%
}\end{mytcolorbox}

\end{document}

[![在此处输入图片描述][3]][3]

对我来说,上述代码的一个关键功能是使用调用前后的和来调整文本上方和下方的间距mytcolorbox\newline%使用早期版本的代码很难实现这种间距。最新版本的代码允许更灵活地在文本上方和下方提供间距,特别是当该文本包含 LaTeX 诗句文本和逐项或枚举列表时。\vspace*{+2mm}%mytcolorboxmytcolorbox

我已经实现了上述代码,以便处理分散在我的项目中的数十个 Locutions(我是这样称呼它们的)。我对未来的选择有一些想法,包括合并\newline%\vspace*{+2mm}%代码(在调用之前和之后mytcolorbox)作为选项mytcolorbox,并包含代码,这些代码对 Locutions 进行编号,类似于表格、图形和方程式的编号方式,并单独列出所有 Locutions 的摘要,例如目前在 LaTeX 中可用的 Locutions。

还要特别感谢 SebGlav 为mytcolorbox我编写的代码提供基础代码。Locutions 工具将成为我的 LaTeX 工具箱中一个有用的故事讲述工具。

如果其他人发现此代码有用,我想知道是否值得创建一个 LaTeX 包?如果是的话,这目前远远超出了我的能力,但我会发现这样的包很有用。

修正早期版本

答案1

编辑:一个tcolorbox版本

根据 OP 的要求,这是一个tcolorbox版本。其实不是两条线但相当容易理解和定制。

带有 tcolorbox 的标题框

\documentclass{article}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage[skins]{tcolorbox}


    
% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\newtcolorbox{mybox}[2]{
    width=#1,
    colframe=black,
    colback=white,
    coltitle=black,
    colbacktitle=white,
    enhanced,
    boxed title style={boxrule=-1pt,left=-2pt,right=-2pt},
    attach boxed title to top center={yshift=-9pt},
    title={#2}}

\begin{document}

\begin{mybox}{11.5cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}
\textrm{%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war. -NY Times”}
\end{mybox}

\end{document}

编辑 2:改进的 TiZ 版本

正如 OP 所建议的,这里有一个版本,其中框的大小直接通过调用它来定义,而不是通过其中的 minipage 来定义。 minipage 的大小取决于调用和自定义inner x sep

如下图所示,我用 13 厘米、11 厘米和 6 厘米的宽度进行了测试,画了一条线,其上方的长度是相应的宽度,现在它完全合适。

标题框改进版本

\documentclass{article}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage[skins]{tcolorbox}

\definecolor{BrickRed}{RGB}{144,44,30} 

\tikzset{mybox/.style={ draw=black, fill=white, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=12pt}}
\tikzset{mytitle/.style={rounded corners,fill=white, text=black, inner sep=0pt}}

\newcommand{\myinnerxsep}{20pt} % Your custom inner x sep for all boxes

\newcommand{\titledbox}[3]
    {
    \begin{tikzpicture}
        \node [mybox,minimum width =#1,inner xsep=\myinnerxsep] (box){%   
            \begin{minipage}{#1-\myinnerxsep-\myinnerxsep}
                #3
            \end{minipage}                
        };
        \node[mytitle] at (box.north) {#2};
    \end{tikzpicture}
    }
        
% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}


\begin{document}

    \titledbox{11cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}{\textrm{%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war. -NY Times”%
\vspace{-2mm}}}

\end{document}

第一个版本(Ti仅限 Z)

这是仅使用 Ti 实现的方法Z.
请注意,使用可以更快地完成此操作tcolorbox

(编辑:我将垂直分隔符固定为 12pt 而不是 20pt,但你可以选择任何适合你需求的分隔符)

标题框的垂直空间较少

\documentclass{article}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{tikz}
\usepackage{varwidth}

\definecolor{BrickRed}{RGB}{144,44,30} 

\tikzset{mybox/.style={ draw=black, fill=white, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=12pt}}
\tikzset{mytitle/.style={rounded corners,fill=white, text=black, inner sep=0pt}}

\newcommand{\titledbox}[3]
    {
    \begin{tikzpicture}
        \node [mybox] (box){%
        \begin{minipage}{#1}        
            #3
        \end{minipage}
        };
        \node[mytitle] at (box.north) {#2};
    \end{tikzpicture}
    }
    
% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\begin{document}
    \titledbox{10.8cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}{\textrm{%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war. -NY Times”%
\vspace{-2mm}}}
\end{document}

答案2

我稍微简化了代码,但主要的变化是使用cell-space-bottom-limit而不是cell-space-limits(设置cell-space-bottom-limitcell-space-top-limit)。

% Note: The following code may need to be run twice
\documentclass{article}
\usepackage{nicematrix}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{tikz}
\usepackage{varwidth}

\definecolor{BrickRed}{RGB}{144,44,30} 


\newenvironment{niceMfbox}[3][-2mm]
  {
    \vspace{#1}%
    \begin{center}
    \begin{NiceTabular}{@{\hspace{20pt}}V{#2}@{\hspace{20pt}}}
      \bfseries \large
      \enspace #3\enspace \\
      \RowStyle[cell-space-bottom-limit=8pt]{}
  }
  {
      \CodeAfter
          \begin{tikzpicture}
          \draw [line width=1pt,rounded corners,BrickRed!75!black] 
            (1-1) -| (last-|2) -- (last-|1) |- (1-1) ;
          \end{tikzpicture}
    \end{NiceTabular}
    \end{center} 
    \par \vspace{2mm}%
  }


% \widetilde{} alternative for formula over the tilde - \SE
\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\begin{document}

\begin{niceMfbox}[+1mm]{10.8cm}{\reallywidetilde{A~\vphantom{p}beginner's~guide\ldots}}%
“Stand-up paddleboarding (SUP, for short) has likely existed for thousands of years. Ancient cultures in South America and Africa stood on small boats with long paddles to travel, fish or go to war. -NY Times”%
\vspace{-2mm}%
\end{niceMfbox}

\end{document}

上述代码的输出

相关内容