自定义描述列表

自定义描述列表

好的伙计们,我有一个稍微奇怪的设置,我希望有人可以帮助我。

我有一个自动生成的.tex 文件(由 pandoc 从 markdown 制作),其中有一个描述列表,如下所示:

\begin{description}
\item[Item One]
This is the text that goes with item one
\item[Item Two]
This one is much nicer
\item[Item Three]
The third item is by far my favourite though.
\end{description}

该块是自动生成的,所以我无法编辑它。

我试图让输出在视觉上匹配不同的文件格式,如下所示:

html_输出

通过使用这个enumitem包,我已经非常接近了。我可以编辑序言,到目前为止我有以下内容:

\usepackage{enumitem}
\setlist[description]{style=multiline,leftmargin=5.5cm, itemsep=0cm}
\renewcommand{\descriptionlabel}[1]{
    \hspace{\labelsep}
    \colorbox{lightgrey}{
        \parbox{4.8cm}{
            \textbf{#1}
        }\par
    }
}

这给了我如下的输出:

pdf_输出

我对 LaTeX 还不太熟悉,我只能做到这些。有人知道我是否可以在项目描述周围添加边框吗?此外,我是否可以连接项目标题并增加文本周围的填充。

谢谢!

- 更新 -

让我重新定义我的问题以使其更清楚。如何让描述项(而不是标签)有边框?基于这个最小的例子:

\documentclass[12pt]{article}

\usepackage{enumitem}
\setlist[description]{style=multiline, leftmargin=5.5cm, itemsep=0cm}

\begin{document}

%% Start of auto-generated code: can't edit this
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[Item One]
This is the text that goes with item one
\item[Item Two]
This one is much nicer
\item[Item Three]
The third item is by far my favourite though.
\end{description}
%% end of auto-generated code

\end{document}

答案1

这是一个使用tikzmarkTikZ 的库和etoolbox自动添加description标签的彩色区域和描述的框架;代码需要运行三次才能稳定:

在此处输入图片描述

代码:

\documentclass[12pt]{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark,calc}

\newcounter{colordesc}
\newlength\longestlabel
\setlength\longestlabel{5.5cm}

\setlist[description]{style=multiline, leftmargin=\longestlabel, itemsep=0cm}

\AtBeginEnvironment{description}{%
  \stepcounter{colordesc}%
  \begin{tikzpicture}[overlay,remember picture]
  \fill[gray!60] 
    ( $ (pic cs:start-\thecolordesc) + (-25pt,-1.2\topsep) $ ) 
      rectangle 
    ( $ ({pic cs:start-\thecolordesc}|-{pic cs:end-\thecolordesc}) + (\longestlabel-25pt,-.5\topsep) $ );
  \draw[gray!60] 
    ( $ (current page text area.east|-{pic cs:start-\thecolordesc}) + (0,-1.2\topsep) $ )
      -- 
    ( $ (current page text area.east|-{pic cs:end-\thecolordesc})  + (0,-0.45\topsep) $ );
  \end{tikzpicture}%
  \tikzmark{start-\thecolordesc}%
}
\AtEndEnvironment{description}{%
  \tikzmark{end-\thecolordesc}%
}
\AtEndEnvironment{description}{%
  \tikz[overlay,remember picture]\draw[gray!60]
    ( $ (current page text area.west|-{pic cs:end-\thecolordesc}) + (0,-.45\topsep) $ ) 
    -- ++(\textwidth,0);%
}

\renewcommand*\descriptionlabel[1]{%
  \tikz\draw[overlay,gray!60]
    (0,.8\baselineskip) -- 
    (\textwidth,.8\baselineskip);%
    \hspace\labelsep\normalfont\bfseries #1%
}

\begin{document}

%% Start of auto-generated code: can't edit this
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[Item One]
This is the text that goes with item one.
\item[Item Two]
This one is much nicer. This one is much nicer. This one is much nicer. This one is much nicer. This one is much nicer. This one is much nicer.
\item[Item Three]
The third item is by far my favourite though.
\item[Item Four]
The fourth item is by far my less favourite.
\end{description}
%% end of auto-generated code

%% Start of auto-generated code: can't edit this
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[Item One]
This is the text that goes with item one. This is the text that goes with item one. This is the text that goes with item one. This is the text that goes with item one.
\item[Item Two]
This one is much nicer.
\item[Item Three]
The third item is by far my favourite though.
\item[Item Four]
The fourth item is by far my less favourite.
\item[Item Fifth]
I don't really care about the fifth item.
\end{description}
%% end of auto-generated code

%% Start of auto-generated code: can't edit this
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[Item One]
This is the text that goes with item one
\item[Item Two]
I don't really care about the second item.
\end{description}
%% end of auto-generated code

\end{document}

评论

就目前的形式而言,此解决方案不能处理分页符;允许分页符所需的修改并不特别困难,只要我有时间(也许这个周末之后),我就会提供它。

相关内容