我需要更改编号列表中多个(完整)的背景\item
。我看到了以下答案如何突出显示整个段落?并(天真地)尝试执行以下操作:
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}
\newcommand{\blob}{A text which is long, but not so long as a
\texttt{\textbackslash lipsum} paragraph and show the
problem about changing the background color}
\begin{document}
\begin{enumerate}
\item \blob
\begin{mdframed}[hidealllines=true,backgroundcolor=blue!20]
\item \blob
\item \blob
\end{mdframed}
\item \blob
\item \begin{mdframed}[hidealllines=true,backgroundcolor=blue!20]
\blob \end{mdframed}
\end{enumerate}
\end{document}
不幸的是,这不起作用。第一种方法mdframed
只是停止列表,第二种使用方法会产生奇怪的效果:
我希望只改变背景,而不改变列表间距。背景变化将要有时发生在分页符处。
我尝试使用在 itemize 环境中将一些项目放在框内但是该方法对于我的应用来说有两个缺点:
- (次要)它只能突出显示列表中的一个部分 --- 如果我需要其中的几个,它就不起作用;
- 如果突出显示的文本跨越页面边界,则(主要)不起作用。
接下来我尝试暂停并恢复列表,但它给我带来了其他问题:
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{framed,xcolor}
\colorlet{shadecolor}{blue!20}
\newcommand{\blob}{A text which is long, but not so long as a
\texttt{\textbackslash lipsum} paragraph and show the
problem about starting and resuming lists multiple times}
\begin{document}
\begin{enumerate}
\item \blob
\end{enumerate}
\begin{shaded}
\begin{enumerate}[resume]
\item \blob
\item \blob
\end{enumerate}
\end{shaded}
\begin{enumerate}[resume]
\item \blob
\item last one
\end{enumerate}
\end{document}
这似乎是一个好主意,但混合环境shaded
会破坏[resume]
选项(可能是由于定义在shaded
环境中的局部性):
答案1
还有进步的空间,但是我已经筋疲力尽了!
使用tcolorbox
:
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable}
\newcommand{\blob}{A text which is long, but not so long as a
\texttt{\textbackslash lipsum} paragraph and show the
problem about changing the background color}
\usepackage{enumitem,lipsum}
\newcommand{\myitem}[1]{%
\end{enumerate}
\begin{tcolorbox}[enhanced,title=,
frame hidden,
colback=blue!20,
breakable,
left=0pt,
right=0pt,
top=0pt,
bottom=4pt,
boxsep=0mm,
arc=0mm,boxrule=0pt,
nobeforeafter,
]
\begin{enumerate}[resume*,itemsep=0pt,topsep=0pt,leftmargin=!]
\item #1
\end{enumerate}
\end{tcolorbox}
\begin{enumerate}[resume*]
}
\begin{document}
\begin{enumerate}
\item \blob
\item \blob
\myitem \blob
\item \blob
\myitem \lipsum
\item \blob %% \myitem can't come at last :-(
\end{enumerate}
\end{document}
使用mdframed
:
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}
\newcommand{\blob}{A text which is long, but not so long as a
\texttt{\textbackslash lipsum} paragraph and show the
problem about changing the background color}
\usepackage{enumitem,lipsum}
\newcommand{\myitem}[1]{%
\end{enumerate}
\begin{mdframed}[hidealllines=true,
backgroundcolor=blue!20,
leftmargin=0,
rightmargin=0,
innerleftmargin=0,
innerrightmargin=0,
skipabove=-3\itemsep,
skipbelow=0]
\begin{enumerate}[resume*,itemsep=0pt,topsep=0pt,]
\item #1
\end{enumerate}
\end{mdframed}
\begin{enumerate}[resume*]
}
\begin{document}
\noindent X\hrulefill X
\begin{enumerate}
\item \blob
\myitem \blob
\item \blob
\myitem \lipsum
\item \blob
\item \blob
\end{enumerate}
\end{document}
注意事项代码很粗糙,还有很大的改进空间。此外,\myitem
不能作为最后一项。
答案2
我找到了一个“解决方法”——我需要定义一个新的环境。下面的代码片段将突出显示 和 之间的列表的所有部分(hlenum
当然,我添加了空格——我应该将它与这里提出的其他解决方案混合在一起,但为时已晚……)。如果将列表更改为普通列表,突出显示将消失。\starthl
\stophl
enumerate
\stophl
不能是列表中的最后一项,但如果您希望突出显示到达列表末尾,则可以省略它。
它的灵感来自于这个答案\global
在设置计数器时添加一个--- 这意味着它只对最外面的列表起作用。
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{framed,xcolor}
\colorlet{shadecolor}{blue!20}%color of the background
\newcommand{\blob}{A text which is long, but not so long as a
\texttt{\textbackslash lipsum} paragraph and show the
problem about changing the background color}
\newcounter{glistresume}
\newif\ifintoshade
\newcommand{\realstarthl}{%
\global\setcounter{glistresume}{\theenumi}
\end{enumerate}
\begin{shaded}
\intoshadetrue
\begin{enumerate}
\setcounter{enumi}{\theglistresume}
}
\newcommand{\realstophl}{%
\global\setcounter{glistresume}{\theenumi}
\end{enumerate}
\end{shaded}
\intoshadefalse
\begin{enumerate}
\setcounter{enumi}{\theglistresume}
}
\newcommand{\starthl}{\relax}
\newcommand{\stophl}{\relax}
\newenvironment{hlenum}{%
\intoshadefalse
\let\starthl\realstarthl
\let\stophl\realstophl
\begin{enumerate}}{%
\end{enumerate}%
\ifintoshade \end{shaded}\fi
}
\begin{document}
\begin{hlenum}
\item \blob
\starthl
\item \blob
\item \blob
\stophl
\item \blob
\starthl
\item \blob
\stophl %% you can comment these two lines
\item last one %% to have an highlight till the end of the list
\end{hlenum}
\end{document}