9

Consider this:

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}

\begin{document}
\begin{mdframed}[innerrightmargin=0pt]
TEXT\end{mdframed}
\end{document}

This produces a frame that is much wider than its content:

enter image description here

I know this is not a bug: the mdframed documentation mentions a userdefinedwidth parameter that defaults to \linewidth. But how can I get a mdframed frame that has the same width as its content?

(What I actually need is a box with rounded corners and shading options; breaking over multiple pages isn't necessary. mdframed seemed like the easiest way to get the box, but if it's too much hassle to make it size to content, it would be useful to know so I can try pure tikz.)

8

How about just creating a command using a tikz node, something like

\newcommand{\myboxedtext}[2][rectangle,draw,fill=orange,rounded corners]{%
            \tikz[baseline=-0.6ex] \node [#1,rounded corners]{#2};}%

This has an optional argument, which can be used as demonstrated in the following MWE.

screenshot

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\newcommand{\myboxedtext}[2][rectangle,draw,fill=orange,rounded corners]{%
        \tikz[baseline=-0.6ex] \node [#1,rounded corners]{#2};}%

\begin{document}
\myboxedtext{boxed text here}
\lipsum[1]

\myboxedtext[fill=red,text=yellow]{boxed text here}
\lipsum[2]
\end{document}
  • What is the purpose of the at (0,0)? – Mohan Dec 30 '12 at 17:11
  • @Mohan good point, it is redundant :) – cmhughes Dec 30 '12 at 17:13
7

Here is one solution that uses varwidth and environ packages. First the \BODY of the environment is saved in a \usebox using the varwidth environment and then its width is measured to specify the \userdefinewidth of the mdframed environment:

enter image description here

Notes:

  • The showframe package was used just to show the page margins. It is not needed in your actual use case.

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage[framemethod=TikZ]{mdframed}
\usetikzlibrary{shadows}
\usepackage{environ}
\usepackage{varwidth}
\usepackage{showframe}

\newlength{\MyMdframedWidthTweak}%
\NewEnviron{MyMdframed}[1][]{%
    \setlength{\MyMdframedWidthTweak}{\dimexpr%
        +\mdflength{innerleftmargin}
        +\mdflength{innerrightmargin}
        +\mdflength{leftmargin}
        +\mdflength{rightmargin}
        }%
    \savebox0{%
        \begin{varwidth}{\dimexpr\linewidth-\MyMdframedWidthTweak\relax}%
            \BODY
        \end{varwidth}%
    }%
    \begin{mdframed}[
        backgroundcolor=lightgray, 
        shadow=true, 
        shadowsize=4pt,
        roundcorner=5pt,
        userdefinedwidth=\dimexpr\wd0+\MyMdframedWidthTweak\relax, 
        #1]
        \usebox0
    \end{mdframed}
}

\begin{document}
\begin{MyMdframed}[backgroundcolor=yellow!20]
    Sample
\end{MyMdframed}
\begin{MyMdframed}[backgroundcolor=green!20]
    Somewhat longer text.
\end{MyMdframed}
\begin{MyMdframed}[backgroundcolor=orange!20]
    Much longer text that takes up more than one line. 
    This should span across the entire width of the page and continue on to the next line.
\end{MyMdframed}
\end{document} 
  • Great solution, it could very well be included in the mdframed package itself as an alternative box. – Joseph Aug 15 '16 at 18:00
1

An example with tcolorbox was missing.

By default, regular tcolorbox uses \linewidth as box width, but the package also provide \tcbox command which adjust box size to its contents. Almost all tcolorbox options can be applied to \tcbox.

Next code shows how to declare \mybox command based on \tcbox. It has one mandatory option which help to change default options and one optional parameter to change default background and frame color.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}

\newtcbox{\mybox}[2][red]{nobeforeafter,tcbox raise base, arc=0pt, outer arc=0pt, colback=#1!10!white, colframe=#1!50!black, boxsep=0pt,left=2pt,right=2pt,top=2pt,bottom=2pt,boxrule=1pt,#2}

\begin{document}

\mybox{}{Default box} \lipsum[4]

\mybox[green]{}{Default with different background}
\lipsum[4]

\mybox{colupper=red!30!black,boxrule=2pt}{Non default box}
\lipsum[4]
\end{document}

enter image description here

0

I think cmhughes is on the right track, but would rather have tikz do the work of aligning the baselines:

\newcommand{\myboxedtext}[2][rectangle,draw,fill=white,rounded corners]{%
            \tikz[baseline] \node [#1,rounded corners,anchor=text]{#2};}%

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.