2

This question is related to Box with Semicircular Parshape at Top and Bottom.

Here I am trying typesetting the top portion into a \savebox using a semicircular \parshape in the MyRoundedBox* environment and this produces:

enter image description here

Whereas the MyRoundedBox environment (ie, non-starred variant), which does not use a \savebox produces:

enter image description here

It seems as if there is some issue with using a \parshape inside a \savebox.

The goal here was to use a \savebox to measure the height of the resultant box (as per @marmot's suggestion in the comments at the linked question). But due to the problem encountered, I simplified the test case further.

Code:

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}
\usepackage{environ}
\usepackage[protrusion=false]{microtype}
%\usepackage{showframe}

\usepackage[mathlines]{lineno}
\linenumbers
\AtEndDocument{\typeout{Total number of lines: \thelinenumber}}

\newcommand*{\Radius}{1.75in}
\newcommand*{\Margins}{2pt}
\newcommand*{\ExtraLeftMargin}{0cm}% Set to 0.5cm if want to see line numbers

\newcommand*{\PaperHeight}{12.5cm}%<----- This is tweaked for this example

\newlength{\TextWidth}
\newlength{\PaperWidth}
\pgfmathsetlength{\TextWidth}{2*\Radius}
\pgfmathsetlength{\PaperWidth}{\TextWidth+2*\Margins}

\usepackage[
    paperwidth=\dimexpr\PaperWidth+\ExtraLeftMargin\relax,
    paperheight=\PaperHeight,
    left=\dimexpr\Margins+\ExtraLeftMargin\relax,
    right=\Margins,
    top=\Margins,
    bottom=\Margins,
]{geometry}

\newcommand*{\TitleAndExternalBorder}[1]{%
    \noindent
    \TitleShape\begingroup\centering\textbf{#1}\par\endgroup%
    \TopSemiCircleParshape%
}

\newcommand*{\TitleShape}{%
    \parshape 2 
    0.40\hsize 0.20\hsize
    0.26\hsize 0.48\hsize 
}
\newcommand*{\TopSemiCircleParshape}{%
    \parshape 9 
    %% First two lines used by title
    %0.40\hsize 0.20\hsize
    %0.26\hsize 0.48\hsize 
    0.20\hsize 0.60\hsize 
    0.14\hsize 0.72\hsize 
    0.10\hsize 0.80\hsize 
    0.07\hsize 0.86\hsize 
    0.05\hsize 0.90\hsize 
    0.03\hsize 0.94\hsize 
    0.025\hsize 0.95\hsize 
    0.01\hsize 0.99\hsize 
    0.00\hsize 1.00\hsize 
}

\newcommand{\TextA}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sem ante, efficitur eget diam quis, lacinia sollicitudin elit. Donec tempor sodales finibus. Fusce accumsan laoreet lorem, eget lacinia urna. Phasellus ornare nibh quis metus dignissim aliquam. Vestibulum hendrerit augue at libero dignissim cursus. Vestibulum tristique metus vitae sem sagittis gravida. Aliquam commodo arcu quam, at congue ex commodo quis. Ut dapibus, quam id suscipit vulputate, orci dolor lobortis lectus, in pellentesque ante est sit amet odio. Integer pretium iaculis sapien sit amet sagittis. Sed sagittis libero libero, eu tempus neque malesuada quis. Aliquam id mauris ac enim tincidunt aliquet nec sit amet odio. In ultricies in nulla id dictum. Nulla facilisi. Proin aliquet semper odio, sit amet eleifend ante.}


\NewEnviron{MyRoundedBox}[1]{%
    %% #1 = Title
    \sloppy\TitleAndExternalBorder{#1}%
    \BODY%
}%

\makeatletter
\newsavebox{\@MyRoundedBoxSavebox}
\NewEnviron{MyRoundedBox*}[1]{% Same as {MyRoundedBox} but uses a \savebox
    %% #1 = Title
    \savebox{\@MyRoundedBoxSavebox}{%
        \sloppy\TitleAndExternalBorder{#1}%
        \BODY%
    }%
    \usebox{\@MyRoundedBoxSavebox}%
}%
\makeatother


\begin{document}%
%% --------------------------------------- This works
%\begin{MyRoundedBox}{My Lipsum Title}
%    \noindent\TextA\par
%\end{MyRoundedBox}%
%% --------------------------------------- This savebox version does NOT
\begin{MyRoundedBox*}{My Lipsum Title}
    \noindent\TextA\par
\end{MyRoundedBox*}%
\end{document}
  • 2
    This is not surprising, since \savebox (like \makebox) typesets its contents in restricted horizontal mode; try something like \savebox{\@MyRoundedBoxSavebox}{\parbox{\textwidth}{...}} (I haven’t tried it myself, however). – GuM Sep 15 '18 at 9:45
  • LaTeX has no \savebox/\sbox like interface for vertical boxes, so simplest is to use TeX syntax. Attention if you use \color inside a \vbox you may need \begingroup/\endgroup then. (which LaTeX adds automatically with \sbox but \sbox creates h-boxes). I think my answer of the linked question also answers this one, possibly, but I did not check the details because as per @GuM comment the problem here is not surprising. – user4686 Sep 15 '18 at 10:40
  • 1
    you need \savebox\mysavebos{\parbox{\linewidth}{....your pararagraph ...} (whether or not your paragraph has a non standard parshape.) – David Carlisle Sep 15 '18 at 11:37
3

Note that \savebox{\name}{...} is equivalent to \setbox\name=\hbox{...}.

BTW, one can also use \unvbox instead of \usebox to allow the glue to expand. This also empties the box.

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}
\usepackage{environ}
\usepackage[protrusion=false]{microtype}
%\usepackage{showframe}

\usepackage[mathlines]{lineno}
\linenumbers
\AtEndDocument{\typeout{Total number of lines: \thelinenumber}}

\newcommand*{\Radius}{1.75in}
\newcommand*{\Margins}{2pt}
\newcommand*{\ExtraLeftMargin}{0cm}% Set to 0.5cm if want to see line numbers

\newcommand*{\PaperHeight}{12.5cm}%<----- This is tweaked for this example

\newlength{\TextWidth}
\newlength{\PaperWidth}
\pgfmathsetlength{\TextWidth}{2*\Radius}
\pgfmathsetlength{\PaperWidth}{\TextWidth+2*\Margins}

\usepackage[
    paperwidth=\dimexpr\PaperWidth+\ExtraLeftMargin\relax,
    paperheight=\PaperHeight,
    left=\dimexpr\Margins+\ExtraLeftMargin\relax,
    right=\Margins,
    top=\Margins,
    bottom=\Margins,
]{geometry}

\newcommand*{\TitleAndExternalBorder}[1]{%
    \noindent
    \TitleShape\begingroup\centering\textbf{#1}\par\endgroup%
    \TopSemiCircleParshape%
}

\newcommand*{\TitleShape}{%
    \parshape 2 
    0.40\hsize 0.20\hsize
    0.26\hsize 0.48\hsize 
}
\newcommand*{\TopSemiCircleParshape}{%
    \parshape 9 
    %% First two lines used by title
    %0.40\hsize 0.20\hsize
    %0.26\hsize 0.48\hsize 
    0.20\hsize 0.60\hsize 
    0.14\hsize 0.72\hsize 
    0.10\hsize 0.80\hsize 
    0.07\hsize 0.86\hsize 
    0.05\hsize 0.90\hsize 
    0.03\hsize 0.94\hsize 
    0.025\hsize 0.95\hsize 
    0.01\hsize 0.99\hsize 
    0.00\hsize 1.00\hsize 
}

\newcommand{\TextA}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sem ante, efficitur eget diam quis, lacinia sollicitudin elit. Donec tempor sodales finibus. Fusce accumsan laoreet lorem, eget lacinia urna. Phasellus ornare nibh quis metus dignissim aliquam. Vestibulum hendrerit augue at libero dignissim cursus. Vestibulum tristique metus vitae sem sagittis gravida. Aliquam commodo arcu quam, at congue ex commodo quis. Ut dapibus, quam id suscipit vulputate, orci dolor lobortis lectus, in pellentesque ante est sit amet odio. Integer pretium iaculis sapien sit amet sagittis. Sed sagittis libero libero, eu tempus neque malesuada quis. Aliquam id mauris ac enim tincidunt aliquet nec sit amet odio. In ultricies in nulla id dictum. Nulla facilisi. Proin aliquet semper odio, sit amet eleifend ante.}


\NewEnviron{MyRoundedBox}[1]{%
    %% #1 = Title
    \sloppy\TitleAndExternalBorder{#1}%
    \BODY%
}%

\makeatletter
\newsavebox{\@MyRoundedBoxSavebox}
\NewEnviron{MyRoundedBox*}[1]{% Same as {MyRoundedBox} but uses a \savebox
    %% #1 = Title
    \setbox\@MyRoundedBoxSavebox=\vbox{%
        \sloppy\TitleAndExternalBorder{#1}%
        \BODY%
    }%
    \noindent\usebox{\@MyRoundedBoxSavebox}%
}%
\makeatother


\begin{document}%
%% --------------------------------------- This works
%\begin{MyRoundedBox}{My Lipsum Title}
%    \noindent\TextA\par
%\end{MyRoundedBox}%
%% --------------------------------------- This savebox version does NOT
\begin{MyRoundedBox*}{My Lipsum Title}
    \noindent\TextA\par
\end{MyRoundedBox*}%
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.