# breakable box with varwidth

PROBLEM

Build an environment to put some text in some boxes that should be both breakable across a page and adapt to text width. Unfortunately, packages mdframed and tcolorboxes have the first option, but not the second, because it seems that varwidth makes them uncomfortable.

PROPOSED SOLUTION

Build two environments.

WORKING EXAMPLE

\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern,textcomp}
\usepackage[italian]{babel}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}

% just for this example
\usepackage{blindtext}

% lists
\usepackage{enumerate}
\renewcommand{\labelitemii}{$\ast$}
\renewcommand{\labelitemiii}{--}

% colors
\usepackage{xcolor}
\definecolor{myblue}{RGB}{0,32,155}
\definecolor{myred}{RGB}{192,0,0}
\definecolor{mygray}{RGB}{68,84,106}
\definecolor{myred2}{RGB}{180,4,38}
\definecolor{myorange}{RGB}{238,132,104}
\definecolor{mygreen}{RGB}{26,152,80}

% url
\usepackage{hyperref}

\usepackage{varwidth}
\usepackage{mdframed}

\mdfdefinestyle{basemdstyle}{%
skipabove=1em,
skipbelow=1em,
innertopmargin=0.75em,
innerbottommargin=0.75em
}
\newmdenv[style=basemdstyle]{baseemph}

% breakable
\newcommand\lredemph[1]{%
\begin{baseemph}[fontcolor=myred]%
{#1}%
\end{baseemph}}

\newcommand\redemph[1]{\fbox{%
\begin{varwidth}{\textwidth}%
{\textcolor{myred}{#1}}%
\end{varwidth}%
}\par}

\begin{document}

\Blindtext

\lredemph{\textbf{this will go across pages.} \Blindtext}

\redemph{this will be just as large as the text}

\end{document}


\documentclass{article}
% General document formatting
\usepackage[margin=0.7in]{geometry}
\usepackage[parfill]{parskip}
% avoid utf8x use utf8 on old latex releases,   use nothing on current releases \usepackage[utf8x]{inputenc}
\usepackage{lmodern}
% not needed in current releases textcomp
\usepackage[italian]{babel}
% Related to math
\usepackage{amsmath,amssymb,amsfonts,amsthm}

% just for this example (not very Italian:-)
\usepackage{blindtext}

% lists
\usepackage{enumerate} % I wrote this, but enumitem is better
\renewcommand{\labelitemii}{$\ast$}
\renewcommand{\labelitemiii}{--}

% colors
\usepackage{xcolor}
\definecolor{myblue}{RGB}{0,32,155}
\definecolor{myred}{RGB}{192,0,0}
\definecolor{mygray}{RGB}{68,84,106}
\definecolor{myred2}{RGB}{180,4,38}
\definecolor{myorange}{RGB}{238,132,104}
\definecolor{mygreen}{RGB}{26,152,80}

% url
\usepackage{hyperref}

\usepackage{mdframed}

\mdfdefinestyle{basemdstyle}{%
skipabove=1em,
skipbelow=1em,
innertopmargin=0.75em,
innerbottommargin=0.75em
}
\newmdenv[style=basemdstyle]{baseemph}

% breakable
%if the text takes multiple lines use mdframed otherwise fbox
\newcommand\lredemph[1]{%
\setbox0\vbox{{#1\par}}%
\ifdim\ht0>\baselineskip
\begin{baseemph}[fontcolor=myred]%
#1%
\end{baseemph}%
\else
\fbox{\textcolor{myred}{#1}}%
\fi}

\begin{document}

\Blindtext

\lredemph{\textbf{this will go across pages.} \Blindtext}

\lredemph{this will be just as large as the text}

\end{document}

• Thanks, that's something I was looking for and makes the use of the newcommand way easier. Sep 12, 2021 at 8:22