在自定义环境中丢失的子弹

在自定义环境中丢失的子弹

我定义了两个带有灰色背景的自定义 enumitem 环境。第一个用于内联项目化,第二个用于普通项目化。第二个失去了亮点,我不知道为什么。

\documentclass[parskip=half]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{tcolorbox}
\usepackage[inline, shortlabels]{enumitem}

% inline itemize
\newenvironment{inlineitem}[1][]
    {% begin-Befehle
        \begin{itemize*}[%
            %before=\hspace{1em},% Abstand vor dem ersten Aufzählungspunkt
            itemjoin=\hspace{1em},% Abstand zwischen zwei Aufzählungspunkten
            #1
        ]
    }
    {% end-Befehle
        \end{itemize*}%
    }
\newenvironment{MaterialInline}[1][]
    {% begin-Befehl
        \begin{tcolorbox}[%
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            arc=0mm,% keinen Rundung in den Ecken
            ]%
        Material:~%
        \begin{itemize*}[%
            itemjoin=\hspace{1em},% Abstand zwischen zwei Aufzählungspunkten
            #1
        ]
    }
    {% end-Befehle
        \end{itemize*}%
        \end{tcolorbox}%
    }
\newenvironment{Material}[1][]
    {% begin-Befehl
        \begin{tcolorbox}[%
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            arc=0mm,% keinen Rundung in den Ecken
            ]%
        \subsubsection{Material}
        \begin{itemize}[%
            #1
        ]
    }
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }
\begin{document}


Done without a custom environment:


\begin{tcolorbox}[%
  grow to left by=2mm,%
  left*=0mm,%
  grow to right by=2mm,%
  right*=0mm,%
  boxrule=0pt,% keine Rahmenlinie
  arc=0mm,% keinen Rundung in den Ecken
  ]
  Material:~%
  \begin{inlineitem}
    \item Item 1
    \item Item 2
    \item Item 3
  \end{inlineitem}
\end{tcolorbox}

The following is done with the custom environment MaterialInline:


\begin{MaterialInline}
  \item Item 1
  \item Item 2
  \item Item 3
\end{MaterialInline}


Material without a custom environment:
\begin{tcolorbox}[%
  grow to left by=2mm,%
  left*=0mm,%
  grow to right by=2mm,%
  right*=0mm,%
  boxrule=0pt,% keine Rahmenlinie
  arc=0mm,% keinen Rundung in den Ecken
  ]
  \subsubsection{Material}
  \begin{itemize}
    \item Item 1
    \item Item 2
    \item Item 3
  \end{itemize}
\end{tcolorbox}


The following is done with the custom environment Material:
\begin{Material}
  \item Item 1
  \item Item 2
  \item Item 3
\end{Material}

\end{document}

在此处输入图片描述

答案1

通过使用

\begin{itemize}[%
            #1
        ]

文本(#1)成为标签,因此没有项目符号。(尝试 *~#1看看会发生什么)。

正确的定义是

 ...
\subsubsection{Material}
    \begin{itemize}
    }
    {% end-Befehle
    \end{itemize}%
\end{tcolorbox}%
}

我猜想当您添加itemjoin= ...用于内联列表的内容时,后面的所有内容都会被忽略,并使用正确的垂直列表。

#1您还可以从 MaterialInline 环境中删除。

A

这是完整的代码。

% !TeX TS-program = pdflatex


\documentclass[parskip=half]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{tcolorbox}
\usepackage[inline, shortlabels]{enumitem}

% inline itemize
\newenvironment{MaterialInline}[1][]
{% begin-Befehl
    \begin{tcolorbox}[%
        grow to left by=2mm,%
        left*=0mm,%
        grow to right by=2mm,%
        right*=0mm,%
        boxrule=0pt,% keine Rahmenlinie
        arc=0mm,% keinen Rundung in den Ecken
        ]%
        Material:~%
        \begin{itemize*}[%
            itemjoin=\hspace{1em},% Abstand zwischen zwei Aufzählungspunkten
            ]
        }
        {% end-Befehle
        \end{itemize*}%
    \end{tcolorbox}%
}
\newenvironment{Material}[1][]
{% begin-Befehl
    \begin{tcolorbox}[%
        grow to left by=2mm,%
        left*=0mm,%
        grow to right by=2mm,%
        right*=0mm,%
        boxrule=0pt,% keine Rahmenlinie
        arc=0mm,% keinen Rundung in den Ecken
        ]%
        \subsubsection{Material}
        \begin{itemize} 
        }
        {% end-Befehle
        \end{itemize}%
    \end{tcolorbox}%
}
\begin{document}    
    
    The following is done with the custom environment MaterialInline:   
    
    \begin{MaterialInline}
        \item Item 1
        \item Item 2
        \item Item 3
    \end{MaterialInline}        
    
    The following is done with the custom environment Material:
    \begin{Material}
        \item Item 1
        \item Item 2
        \item Item 3
    \end{Material}
    
\end{document}

答案2

在 environment 的定义中Material,你可以判断它的可选参数是否为空,只有\begin{itemize}[#1]#1其非空时才使用。例如

\usepackage{etoolbox} % already loaded by `tcolorbox`
\newenvironment{Material}[1][]
    {% begin-Befehl
        \begin{tcolorbox}[...]%
        \subsubsection{Material}
        \ifstrempty{#1}
          {\begin{itemize}}
          {\begin{itemize}[#1]}%
    }
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }

这解决了缺少项目符号的问题,但又引入了一个新问题:现在\begin{Material} \item ...无法区分\begin{Material}[] \item ...,并且您永远无法设置空列表标签。

为了解决这个问题,我建议使用 的xparsearg-type 。它区分可选参数的o“缺失”( \begin{Material} \item ...) 和“用户给出”( )。\begin{Material}[<any value, even emtpy>] \item ...

\NewDocumentEnvironment{Material}{o}
    {% begin-Befehl
        \begin{tcolorbox}[...]%
        \subsubsection{Material}
        \IfValueTF{#1}
          {\begin{itemize}[#1]} % optional #1 is given by user
          {\begin{itemize}}     % optional #1 has no value
    }
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }

相关内容