如何设置 mdframed 的宽度?

如何设置 mdframed 的宽度?

我是乳胶新手,我想设置宽度和高度mdframed。有人可以帮我完成这个任务吗?

\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. 我当前的输出是, 在此处输入图片描述

  2. 我的预期输出是

在此处输入图片描述

答案1

这是一个使用的解决方案varwidthenviron 包。首先\BODY将环境的保存在\usebox使用varwidth环境中,然后测量其宽度以指定环境\userdefinewidthmdframed

在此处输入图片描述

笔记:

代码:

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

相关内容