Consider this:



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.)


4 Answers 4


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.



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

\myboxedtext{boxed text here}

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

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


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




    Somewhat longer text.
    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.
  • 1
    Great solution, it could very well be included in the mdframed package itself as an alternative box.
    – user9424
    Commented Aug 15, 2016 at 18:00

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.


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


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

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

\mybox{colupper=red!30!black,boxrule=2pt}{Non default box}

enter image description here


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

You must log in to answer this question.

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