TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
If you ask TeX to break the lines then the size of the content is \linewidth even if some of the content is white. If you just want a box of natural size then \fbox{TEXT} would work, or others will show you fancy Tikz Boxes:-) – David Carlisle Dec 30 '12 at 16:51
Thanks -- I got decent results with pure tikz. (draw=black!30, fill=gray!4, inner sep = 3mm, outer sep = 3mm, rounded corners=5mm) – Mohan Dec 30 '12 at 16:58
Feel free to "self answer" to round off the question. (Tikz is far too modern for me:-) – David Carlisle Dec 30 '12 at 17:04
Possible Duplicate/Related Questions: A \boxed alternative with nicer spacing?, A \boxed alternative with minimal spacing. – Peter Grill Dec 30 '12 at 18:43
up vote 7 down vote accepted

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}
share|improve this answer
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

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.
share|improve this answer

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

share|improve this answer

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};}%
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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