![如何方便地在 itemize 中为偶数项或奇数项加下划线?](https://linux22.com/image/341749/%E5%A6%82%E4%BD%95%E6%96%B9%E4%BE%BF%E5%9C%B0%E5%9C%A8%20itemize%20%E4%B8%AD%E4%B8%BA%E5%81%B6%E6%95%B0%E9%A1%B9%E6%88%96%E5%A5%87%E6%95%B0%E9%A1%B9%E5%8A%A0%E4%B8%8B%E5%88%92%E7%BA%BF%EF%BC%9F.png)
我想在itemize
环境中为偶数项或奇数项加下划线。有没有比以下更方便的方法:
\newcommand{\uu}{\underline}
\begin{itemize}
\item \uu{some text}
\item some text
\item \uu{some text}
\item some text
\item \uu{some text}
\item some text
\end{itemize}
答案1
请注意,\item
已重新定义为将文本视为参数。此外,这仅支持一个级别。
\documentclass{article}
\newcommand{\myitem}[1]% text must be placed in braces
{\olditem\stepcounter{enumi}%
\ifodd\value{enumi}\underline{#1}
\else{#1}\fi}
\newenvironment{myitemize}{\itemize
\setcounter{enumi}{0}%
\let\olditem=\item
\let\item=\myitem}{\enditemize}
\begin{document}
\begin{myitemize}
\item{odd text}
\item{even text}
\end{myitemize}
\end{document}
答案2
您可以在每次出现时拆分环境的内容,这样\item
您就可以分离出每个列表项的“参数”。这样,您就可以(有条件地)对执行操作\item
:
\documentclass{article}
% https://tex.stackexchange.com/a/127014/5764
\usepackage{xparse,environ,soul}
\ExplSyntaxOn
\NewEnviron{oddevenitemize}[1][]
{
\int_zero:N \l_odd_even
\keys_set:nn { oddeven/itemize } { #1 }
\seq_set_split:NnV \l_oddeven_itemize_input_seq { \item } \BODY
\seq_pop_left:NN \l_oddeven_itemize_input_seq \l_tmpa_tl
\tl_use:N \l_oddeven_itemize_pre_tl
\seq_set_map:NNn
\l_oddeven_itemize_output_seq
\l_oddeven_itemize_input_seq
{ \exp_not:n { \__oddeven_itemize_do:n { ##1 } } }
\seq_use:NV \l_oddeven_itemize_output_seq \l_oddeven_itemize_sep_tl
\tl_use:N \l_oddeven_itemize_post_tl
}
\seq_new:N \l_oddeven_itemize_input_seq
\seq_new:N \l_oddeven_itemize_output_seq
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\cs_generate_variant:Nn \seq_use:Nn { NV }
\int_new:N \l_odd_even
\keys_define:nn { oddeven/itemize }
{
pre .tl_set:N = \l_oddeven_itemize_pre_tl,
post .tl_set:N = \l_oddeven_itemize_post_tl,
sep .tl_set:N = \l_oddeven_itemize_sep_tl,
action .code:n = \cs_set_eq:NN \__oddeven_itemize_do:n #1,
action .initial:n = \use:n,
}
\NewDocumentCommand{\normalitem}{m}{%
\item #1
}
\NewDocumentCommand{\underlineodd}{m}{%
\int_incr:N \l_odd_even
\item
\int_if_odd:nTF { \l_odd_even }
{\ul{#1}}% Underline odd
{#1}% Do nothing for even
}
\NewDocumentCommand{\underlineeven}{m}{%
\int_incr:N \l_odd_even
\item
\int_if_odd:nTF { \l_odd_even }
{#1}% Do nothing for odd
{\ul{#1}}% Underline even
}
\ExplSyntaxOff
\begin{document}
\begin{oddevenitemize}[
action=\underlineodd,
pre=\begin{itemize},
post=\end{itemize},
]
\item First
\item Second
\item Third
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed semper eros sed quam ullamcorper iaculis tincidunt feugiat neque.
Donec ut risus at arcu tristique porttitor. Proin sollicitudin
sollicitudin aliquam. Mauris rutrum, erat et gravida ornare, odio
justo aliquet ipsum, at venenatis sem quam fermentum tortor.
\item Integer et ante non libero scelerisque hendrerit at nec dui.
Proin ante velit, finibus id tempor id, laoreet eu quam.
Suspendisse semper ex id magna luctus egestas.
Phasellus pharetra nibh bibendum felis commodo pulvinar.
\item Last
\end{oddevenitemize}
\end{document}
项目使用 进行格式化action
。在上面的例子中,我们使用\underlineodd
,但您可以将其切换为\underlineeven
以给偶数\item
s 加下划线。
下划线是在以下人员的支持下完成的soul
允许换行。
参考: