4

Problem Description

Need help to see what I am missing with the following NiceTabular box that has a bit too much spacing above the text.

MWE:

% 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}

Desired Output/Solution

I'd like to close up the spacing between the box title A beginner's guide ... and the text inside the box “Stand-up paddleboarding...". May not seem like much, but it has been gnawing at me that I cannot reduce the spacing. I've tried many permutations and convolutions of \vspace(-somenumber) all to no avail, enough to know I'm out of my league! NiceTabular box w too much spacing. Any and all thoughts appreciated ...

Update:

First: Thank you to both authors of their respective solutions to my posting!

Second: I have a bounty of having not one, but two solutions to improve my code. This is not one of [Solomon's]2 difficult choices as I have a few dozen places in my story line where I have used my niceMfbox code. So I will run a test of each of the two solutions in those few dozen places in my story line to determine which solution performs best overall. Then I will vote up to confirm one of the two solutions as the 'best' solution. My choice, however, may not be 'best' for ALL LaTeX applications, but having each solution so well detailed here could help others in the future with their particular LaTeX application.

Update using SebGlav's tcolorbox code:

I've adjusted SebGlav's tcolorbox LaTeX code to suit my particular needs, but hands down it is by far the best solution out of all of SebGlav's answers. My hat is off to all of SebGlav's work to bring this to the best solution I could have imagined. Thank you SebGlav. Now the MWE of what I finally will be using in my project:

% 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}

[![enter image description here][3]][3]

For me, a key feature with the above code is to adjust the spacing above and below the text within the mytcolorbox using both the \newline% and the \vspace*{+2mm}% before and after the call to mytcolorbox. Such spacing was very difficult with the earlier versions of the code. The latest version of the code allows for more flexibility to provide spacing above and below the text within the mytcolorbox, particularly when that text includes LaTeX verse text, and itemized or enumerated lists.

I've implemented the above code to work with the dozens of Locutions (as I refer to them) I have scattered throughout my project. I have a few ideas for future options including the incorporation of the \newline% and the \vspace*{+2mm}% code (before and after the call to mytcolorbox) to be included as options for mytcolorbox, and to include code that numbers the Locutions similar to way tables, figures and equations are numbered, with separate summary listings of all Locutions such as is currently available with LaTeX.

A separate Thank You to SebGlav for providing the base code for the mytcolorbox I have coded. The Locutions tool will be a useful story telling device in my LaTeX box of tools.

If others find this code useful, I wonder if it would be worth creating a LaTeX package? If so, it's currently far beyond my capability, but I would find such a package useful.

Corrected earlier version

3
  • Not related but I don't understand why you use nicematrix to do this kind of board which is very easy to do with two lines of TikZ.
    – SebGlav
    Aug 16, 2022 at 10:28
  • 1
    I'd gladly replace my convoluted code with "two lines of TikZ". Any links or references greatly appreciated! Aug 16, 2022 at 10:32
  • You should use cell-space-bottom-limit instead of cell-space-limits. Aug 16, 2022 at 11:51

2 Answers 2

5

EDIT: a tcolorbox version

As per request from OP, here's a tcolorbox version. Not really two lines but quite easy to understand and to customize.

titled box with 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}

EDIT 2: an improved TikZ version

As suggested by OP, here's a version where the size of the box is directly defined by calling it and not by the minipage in it. The minipage is sized depending on the calling and on a custom inner x sep.

As you can see in the picture below, I tested it with widths of 13 cm, 11cm and 6 cm, drawing a line above which length is the corresponding width, and it fits perfectly now.

titled box improved version

\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}

First version (TikZ only)

Here's a way to do it only with TikZ.
Note that this could be done faster using tcolorbox.

(edit: I fixed the vertical sep to 12pt instead of 20pt, but you can chose whatever fits your need)

titled box with less vertical space

\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}
11
  • 1
    If you'd like to suggest a solution using tcolorbox, I can include that solution as I test of all the solutions within my story line to determine which solution performs best overall. Just a thought ... thank you again for your solution! Aug 17, 2022 at 0:52
  • 1
    Thanks ... still working with your TikZ version ... having a few issues that I'm trying to work out ... Thank you for the tcolorbox version ... I'll have a look ! Aug 17, 2022 at 14:10
  • 1
    Thanks ... not enough sleep ... but I'm making progress ... so far your TikZ version seems to work nicely ... so I'm looking forward to trying your tcolorbox version. Thanks again ... I'll comment here when I'm done ... but I may need to get some sleep in the meantime! Bye for now! Aug 17, 2022 at 15:29
  • 1
    As soon as I've finished implementing your tcolorbox version in my project, I'll likely accept your answer to my OP. Thank you again for all your help! Aug 18, 2022 at 14:09
  • 1
    Apologies for taking so long to get back, but I've completed my additions to your tcolorbox version of the code, which is far better that the earlier versions, particularly with respect to providing spacing before and after the text. I may post a follow-up question as to how to include the \newline% and the \vspace*{+2mm}% code that I now include to provide spacing before and after mytcolorbox, as options within the coding for mytcolorbox itself. Thank you for all your efforts! Aug 23, 2022 at 3:26
5

I have simplified a bit the code but the main change is the use of cell-space-bottom-limit instead of cell-space-limits (which sets both cell-space-bottom-limit and cell-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}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .