Tell me more ×
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
show 1 more comment

1 Answer

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

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.