6

Do you know how to force mdframed to frame text more tightly? It seems, that it is just framing the whole line. But my goual is to have just framed text, so that the finished boxes will have different width on the basis of text. If you look on the picture, you will understand, what I mean. Thanks alot for help!

\mdfdefinestyle{MyFrame}{%
    linecolor=black,
    outerlinewidth=2pt,
    %roundcorner=20pt,
    innertopmargin=4pt,
    innerbottommargin=4pt,
    innerrightmargin=4pt,
    innerleftmargin=4pt,
        leftmargin = 4pt,
        rightmargin = 4pt
    %backgroundcolor=gray!50!white}
        }

% text
\begin{mdframed}[style=MyFrame,nobreak=true]
\begin{center}
\textbf{THIS TEXT SHOULD BE MORE TIGHTLY FRAMED }
\end{center}
\end{mdframed}

% sipka
\begin{figure}[h!]
\centering
\includegraphics[width=.1\textwidth]{sipka3.pdf}
\end{figure}
% arrow
\begin{mdframed}[style=MyFrame,nobreak=true]
\lipsum[2]
\end{mdframed}

% sipka
\begin{figure}[h!]
\centering
\includegraphics[width=.1\textwidth]{sipka3.pdf}
\end{figure}
% arrow
\begin{mdframed}[style=MyFrame,nobreak=true]
\lipsum[1]
\end{mdframed}

enter image description here

2 Answers 2

9

You can use the mdframed options align=center,userdefinedwidth=....

enter image description here

\documentclass{article}
\usepackage{mdframed,lipsum}
\begin{document}
\mdfdefinestyle{MyFrame}{%
    linecolor=black,
    outerlinewidth=2pt,
    %roundcorner=20pt,
    innertopmargin=4pt,
    innerbottommargin=4pt,
    innerrightmargin=4pt,
    innerleftmargin=4pt,
        leftmargin = 4pt,
        rightmargin = 4pt
    %backgroundcolor=gray!50!white}
        }

% text
\lipsum[1]
\begin{mdframed}[style=MyFrame,nobreak=true,align=center,userdefinedwidth=30em]
\textbf{THIS TEXT SHOULD BE MORE TIGHTLY FRAMED}
\end{mdframed}
\lipsum[2]
\end{document}
5

To improve gernot's answer, you can use the calc package to automatically figure out the required width, i.e,

\documentclass{article}
\usepackage{mdframed,lipsum,calc}
\begin{document}
\mdfdefinestyle{MyFrame}{%
    linecolor=black,
    outerlinewidth=2pt,
    %roundcorner=20pt,
    innertopmargin=4pt,
    innerbottommargin=4pt,
    innerrightmargin=4pt,
    innerleftmargin=4pt,
        leftmargin = 4pt,
        rightmargin = 4pt
    %backgroundcolor=gray!50!white}
        }

\newcommand\header[1]{
  \newlength{\headerwidth}
  \setlength{\headerwidth}{\widthof{#1}}
  \addtolength{\headerwidth}{8pt}
  \begin{mdframed}[style=MyFrame,nobreak=true,align=center,userdefinedwidth=\headerwidth]
    #1
  \end{mdframed}
}


% text
\lipsum[1]
\header{\textbf{THIS TEXT SHOULD BE MORE TIGHTLY FRAMED}}
\lipsum[2]
\end{document}

I put the environment in a new command, since I needed to use its content twice. I added 8pt manually, since I'm not sure how else to extract the innerleftmargin and innerrightmargin from the style. Experimenting with different strings of text, it doesn't seem to handle the margins right; I don't know why. In my own code, I just add an extra \qquad to the text to account for the margins.

You must log in to answer this question.

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