mdframed 在 itemize 环境中

mdframed 在 itemize 环境中

我希望mdframed在文档的常规主体中使用时和在环境中使用时拥有相同宽度的环境itemize

是否可以?

以下是 MWE:

\documentclass[a4paper]{article}
\usepackage[tikz]{mdframed}

\newmdenv[%
leftmargin = 2cm,
rightmargin = 2cm,
middlelinewidth = 0.3mm,
align=center,
backgroundcolor = lightgray]
{myframe}
\begin{document}

\begin{myframe}
Test 1
\end{myframe}

\begin{itemize}
\item This is some text

\begin{myframe}
Test 2
\end{myframe}

But I would like, if possible, the two framed to have the same size, and also to aligned, even if one is in an \verb+itemize+ environment.
\end{itemize}

\end{document}

在此处输入图片描述

答案1

我会使用 tcolorbox。它的表现比 mdframed 好得多。

\documentclass[a4paper]{article}
\usepackage[many]{tcolorbox}

\newtcolorbox
{myframe}
{left skip = 2cm-\csname @totalleftmargin\endcsname,
right skip = 2cm,
}

\begin{document}
blblblbb

\begin{myframe}
Test 1
\end{myframe}

\begin{itemize}
\item This is some text 

\begin{myframe}
Test 2
\end{myframe}

But I would like, if possible, the two framed to have the same size, and also to aligned, even if one is in an \verb+itemize+ environment.
\end{itemize}

\end{document}

在此处输入图片描述

答案2

您可以使用\AtBeginEnvironment{itemize}{}来调整leftmargin=您处于某个itemize环境中时的情况:

在此处输入图片描述

笔记:

  • 我删除了该align=center选项,因为你不能让框架居中\linewidth \linewdith当发生改变时(因为它在环境中) ,也会在两个边缘上对齐itemize

代码:

\documentclass[a4paper]{article}
\usepackage[tikz]{mdframed}
\usepackage{enumitem}
\usepackage{calc,showframe}

\newlength{\MyLeftMargin}
\setlength{\MyLeftMargin}{\leftmargin}
\AtBeginEnvironment{itemize}{%
    \setlength{\MyLeftMargin}{0cm}%
}

\newmdenv[%
leftmargin = \MyLeftMargin,
rightmargin = \leftmargin,
middlelinewidth = 0.3mm,
%align=center,
backgroundcolor = lightgray,
]
{myframe}
\begin{document}

\begin{myframe}
Test 1 
\end{myframe}

\begin{itemize}
\item This is some text 

\begin{myframe}
Test 2 
\end{myframe}

But I would like, if possible, the two framed to have the same size, and also to aligned, even if one is in an \verb+itemize+ environment.
\end{itemize}

\end{document}

相关内容