5

I am new to latex i want to set a width and height of the mdframed. can any one help me to achieve this task.

\documentclass{article}
\usepackage{xcolor}
\usepackage{mdframed}
\newtheorem{mdtheorem}{Theorem}
 \newenvironment{theorem}%
 {\begin{mdframed}[backgroundcolor=lightgray]\begin{mdtheorem}}%
 {\end{mdtheorem}\end{mdframed}}
\begin{document}
\begin{theorem}
Sample
\end{theorem}
\end{document} 
  1. My current output is , enter image description here

  2. My expected output is

enter image description here

cmhughes
  • 100,947
PSK
  • 1,032

1 Answers1

5

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:

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{environ}
\usepackage{varwidth}
\usepackage{showframe}

\newlength{\TheoremWidthTweak}%

\NewEnviron{theorem}[1][]{% \setlength{\TheoremWidthTweak}{\dimexpr% +\mdflength{innerleftmargin} +\mdflength{innerrightmargin} +\mdflength{leftmargin} +\mdflength{rightmargin} }% \savebox0{% \begin{varwidth}{\dimexpr\linewidth-\TheoremWidthTweak\relax}% \BODY \end{varwidth}% }% \begin{mdframed}[backgroundcolor=lightgray,userdefinedwidth=\dimexpr\wd0+\TheoremWidthTweak\relax, #1] \usebox0 \end{mdframed} }

\begin{document} \begin{theorem}[backgroundcolor=yellow!20] Sample \end{theorem} \begin{theorem}[backgroundcolor=green!20] Somewhat longer text. \end{theorem} \begin{theorem}[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{theorem} \end{document}

Peter Grill
  • 223,288