我怎样才能使描述性列表风格化,并在标签和描述之间添加一条线?

我怎样才能使描述性列表风格化,并在标签和描述之间添加一条线?

我希望我的描述列表看起来像......

在此处输入图片描述

其来源是(来源)...

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins,hooks}

\newcommand\fancybox[3]{%
\tcbset{
    mybox/.style={
        enhanced,
        boxsep=0mm,
        opacityfill=0,
        overlay={
            \coordinate (X) at ([xshift=-1mm, yshift=-1.5mm]frame.north west);
            \node[align=right, text=#1, text width=2.5cm, anchor=north east] at (X) {#2};
            \draw[line width=0.5mm, color=#1] (frame.north west) -- (frame.south west); 
            }
        }
    }
\begin{tcolorbox}[mybox]
    #3
\end{tcolorbox}
}

\definecolor{MyPink}{HTML}{7F007F}
\definecolor{MyGrey}{HTML}{838383}
\definecolor{MyGreen}{HTML}{02856B}

\begin{document}

\fancybox{MyPink}{Define Situation}{
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
}

\end{document} 

有没有办法可以用这个 fancybox 环境替换默认的描述列表项(或者以其他方式添加行分隔符)?

我尝试过,\renewcommand{\descriptionlabel[1]{\fancybox{red}{#1}{}}并将标签设置在正确的位置,但我无法弄清楚如何正确设置描述。

编辑!

我不认为使用这个环境是最好的方法。相反,我尝试改编来自这个帖子得到某物…

我最终得到了......

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

\setlist[description]{style=multiline, leftmargin={\dimexpr 2.5cm+\labelwidth+\labelsep}}

\newcommand\desclist[2]{%
  \begin{tikzpicture}
    \node[align=right, anchor=east] (label) at (0,0) {#1};
    \node[anchor=west, text width=\dimexpr\linewidth-1.5em] at ($(label.east)+(1.5em,0)$) {#2};
    \draw[line width=1.5pt] ($(label.north east)+(-2pt,2pt)$) -- ($(label.south east)+(-2pt,-2pt)$);
  \end{tikzpicture}%
}

\renewcommand\descriptionlabel[1]{\desclist{#1}{}}


\begin{document}

%% Start of auto-generated code: can't edit this
\begin{description}
\item[Item One]
This is the text that goes with item one.
\end{description}
\end{document}

看起来几乎正确但并不完全正确......

答案1

以下更像是一种 hack。它不使用description环境,而是使用enumitem修补的环境。您可以对每个项目itemize使用 newdefined 命令:\fancybox

\documentclass[]{report}
\usepackage[T1]{fontenc}

\usepackage{blindtext}

\usepackage{tcolorbox}
\tcbuselibrary{skins,hooks}

\newcommand\fancybox[3]{%
    \tcbset{
        mybox/.style={
            enhanced,
            boxsep=0mm,
            opacityfill=0,
            overlay={
                \coordinate (X) at ([xshift=-1mm, yshift=-1.5mm]frame.north west);
                \node[align=right, text=#1, text width=2.5cm, anchor=north east] at (X) {#2};
                \draw[line width=0.5mm, color=#1] (frame.north west) -- (frame.south west); 
            }
        }
    }
    \begin{tcolorbox}[mybox]
        #3
    \end{tcolorbox}
}

\definecolor{MyPink}{HTML}{7F007F}
\definecolor{MyGrey}{HTML}{838383}
\definecolor{MyGreen}{HTML}{02856B}

\usepackage{enumitem}
\setlist[itemize,1]{label={},leftmargin={\dimexpr 2.5cm+\labelwidth+\labelsep}} % add width of descriptionlabel and needed widths for itemize indent of \items to the margin. Thus, the environment is correct indented

\begin{document}
\blindtext
\begin{itemize}
    \item \fancybox{MyPink}{Define Situation}{
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
    }
    \item \fancybox{MyGreen}{2nd Situation}{\blindtext}
\end{itemize}
\blindtext
\end{document}

它看起来像这样:

描述

如果您希望此版本仅用于描述itemize环境的一个特定情况,请注释掉该\setlist...行并将选项添加到特定环境:\begin{itemize}[label={},leftmargin={\dimexpr 2.5cm+\labelwidth+\labelsep}] ...

这样,您也可以itemize在同一个文档中使用常规环境。

我希望这是一个有用的解决方案,尽管并不完美;)

编辑:

您可以将其包装在新定义的环境中,甚至可以添加可选参数,例如在以下情况下设置的长度leftmargin。此外,我定义了一个新的长度,\descrboxlabelwidth以便能够更改标签框的宽度:

\documentclass[10pt]{report}
\usepackage[T1]{fontenc}

\usepackage{blindtext}

\usepackage{tcolorbox}
\tcbuselibrary{skins,hooks}

\definecolor{MyPink}{HTML}{7F007F}
\definecolor{MyGrey}{HTML}{838383}
\definecolor{MyGreen}{HTML}{02856B}

\usepackage{enumitem}
\newlength{\descrboxlabelwidth}
\setlength\descrboxlabelwidth{2,5cm}
\NewDocumentEnvironment{descrlist}{O{+\labelwidth+\labelsep}}{
    \begin{itemize}[label={},leftmargin={\dimexpr \descrboxlabelwidth#1}]
    }
    {\end{itemize}}
\newcommand\fancybox[3]{%
    \tcbset{
        mybox/.style={
            enhanced,
            boxsep=0mm,
            opacityfill=0,
            overlay={
                \coordinate (X) at ([xshift=-1mm, yshift=-1.5mm]frame.north west);
                \node[align=right, text=#1, text width=\descrboxlabelwidth, anchor=north east] at (X) {#2};
                \draw[line width=0.5mm, color=#1] (frame.north west) -- (frame.south west); 
            }
        }
    }
    \begin{tcolorbox}[mybox]
        #3
    \end{tcolorbox}
}
\begin{document}
    \blindtext
    \begin{descrlist}
        \item \fancybox{MyGreen}{2nd Situation}{Lorem Ipsum is simply dummy text of the printing and typesetting industry.}
        \item \fancybox{MyPink}{Define Situation}{
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
        }
    \end{descrlist}
    \blindtext
    \begin{descrlist}[+.4\linewidth]
        \item \fancybox{MyGreen}{2nd Situation}{Lorem Ipsum is simply dummy text of the printing and typesetting industry.}
        \item \fancybox{MyPink}{Define Situation}{
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
        }
        \item \fancybox{MyPink}{Define Situation}{
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
        }
        \item \fancybox{MyPink}{Define Situation}{
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
        }
        \item \fancybox{MyPink}{Define Situation}{
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley 
        }
    \end{descrlist}
\end{document}

另外,这是页面可破坏的,这似乎是你的一个未解决的问题提及帖子

相关内容