![在自定义环境中丢失的子弹](https://linux22.com/image/450719/%E5%9C%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%B8%A2%E5%A4%B1%E7%9A%84%E5%AD%90%E5%BC%B9.png)
我定义了两个带有灰色背景的自定义 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 环境中删除。
这是完整的代码。
% !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 ...
,并且您永远无法设置空列表标签。
为了解决这个问题,我建议使用 的xparse
arg-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}%
}