具体来说,我需要编写一个命令,如果选项wide
打开,它会改变其行为......所以我需要某种条件
if wide is on then do this, else do that.
有人能指出如何在 TeX 中实现这种情况吗?
伪代码:
\documentclass{article}
\usepackage{enumitem}
\begin{document}
\begin{itemize}[wide]
\item
\if@wide \do "print{this list is wide} and change \rightmargin to 3cm"
\else \do "print{this list is not wide} and change \rightmargin to 2cm"
\fi
\end{itemize}
\end{document}
答案1
您可以挂钩到选项的定义wide
并使条件为真。条件的值在环境之后重置。
还请注意,该wide
选项带有参数。如果没有传递参数,则\parindent
使用 (IE, \begin{itemize}[wide]
是相同的\begin{itemize}[wide=\parindent]
)。
但是,您不应该尝试enumitem
在文档中更改 的内部参数(它们之所以是内部参数是有原因的)。您应该坚持使用它提供的键值接口。
无论如何,这里是:
\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\makeatletter
\newif\ifwide
\patchcmd\enitkv@enumitem@wide
{\enit@align@left}% <- Search
{\widetrue\enit@align@left}% <- Replace
{}{\FailedToPatch}
\makeatother
\begin{document}
\begin{itemize}[wide]
\item
\ifwide \texttt{wide} was used
\else \texttt{wide} was NOT used
\fi
\end{itemize}
\begin{itemize}
\item
\ifwide \texttt{wide} was used
\else \texttt{wide} was NOT used
\fi
\end{itemize}
\end{document}
答案2
这是您可以使用宏设置中包含的列表样式的一种方法。
\documentclass{article}
\usepackage{enumitem,lipsum}
\newenvironment{styledlist}[1][]
{\expandafter\itemize\expandafter[#1]}{\enditemize}
\def\widelist{wide,itemindent=15pt,label=*,leftmargin=-1cm,rightmargin=-1cm}
\def\narrowlist{itemindent=15pt,label=++,leftmargin=1cm,rightmargin=1cm}
\begin{document}
\begin{styledlist}[\widelist]
\item \lipsum[4]
\end{styledlist}
\begin{styledlist}[\narrowlist]
\item \lipsum[4]
\end{styledlist}
\end{document}