Take the 2-minute tour ×
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.

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

share|improve this question
3  
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
    

4 Answers 4

up vote 6 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.

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

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

\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

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

 
discard

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.