1

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}

    % useful comments
    \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}}

    % width-adaptive
    \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}

1 Answer 1

2

enter image description here

comments added inline

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

    % useful comments
    \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}
1
  • Thanks, that's something I was looking for and makes the use of the newcommand way easier.
    – brodegon
    Sep 12, 2021 at 8:22

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.