3

I would like to have a mdframed environment of with the same width when it is used in the normal body of my document and when it is used in an itemize environment.

Is it possible?

Here is a 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}

enter image description here

  • 1
    Could you post a minimal code showing your problem? – Bernard Sep 30 '17 at 19:16
  • 1
    Possibly {\leftskip=0pt ...}. Can't test it without an MWE. – John Kormylo Oct 1 '17 at 1:51
  • Hi, Thanks for your reply. I have added a MWE. – Colas Oct 1 '17 at 16:27
6
+50

I would use tcolorbox. It behaves much better than 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}

enter image description here

  • Thanks Ulrike. Do you mean tcolorbox is better generally (according to you)? Or just in this case? – Colas Oct 4 '17 at 20:12
  • 2
    In general. It is more powerful, works better, is actively maintained. – Ulrike Fischer Oct 4 '17 at 20:15
3

You can use \AtBeginEnvironment{itemize}{} to adjust the leftmargin= when you are within an itemize environment:

enter image description here

Notes:

  • I removed the align=center option as you can't have the frame centered with the \linewidth and also be aligned on both margins when the \linewdith is altered (as it is within the itemize environment).

Code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.