我是乳胶新手,我想设置宽度和高度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
这是一个使用的解决方案varwidth
和environ
包。首先\BODY
将环境的保存在\usebox
使用varwidth
环境中,然后测量其宽度以指定环境\userdefinewidth
的mdframed
:
笔记:
- 包裹
showframe
仅用于显示页边距。实际使用中不需要它。 - 这是解决方案的一个稍微修改的版本mdframed:根据内容调整框架大小。
代码:
\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}