如何在我的列表环境中设置边距和位置

如何在我的列表环境中设置边距和位置

我对 (La)TeX 的宏和自定义环境等东西还不太熟悉,所以我不确定我是否使用了正确的方法。我想创建自己的列表环境,它应该会产生类似于此处列表的内容(但我不想enumitem为其添加更多类似的包):

\begin{pathlist}
    \item[\texttt{nodes/\{id\}}]
    Gives a single node by its id.

    \item[\texttt{nodes/\{id\}/children}]
    Gives all children of a node, also if they are stored in a field, which is not called {\sf childIds}.
    %snip
\end{pathlist}

结果如下:

enter image description here

因此它应该成为一个可操纵的列表。但我如何控制和description的边距和位置\descriptionlabel条目主体(我不知道该命令)?


正如您在现场看到的那样,标签放置得很糟糕,它超出了正常文本主体的边缘(左侧),但是条目主体位于其最右边。

我当前的代码:

%my own description list
%newenvironment takes as definition:
%{description}[numberOfArguments]{before, processed before text in environment is processed}{after, see @ before}
\definecolor{darkblue}{HTML}{0304A4}
\newenvironment{pathlist}{%
\renewcommand{\descriptionlabel}[1]{%
        %\leftmargin{5px}
        \parbox[b][2em][c]{\textwidth}{{\color{darkblue}\textsf{##1}}}}%
    \begin{description}}
    {\end{description}}

在那里使用\leftmargin根本行不通。或者,我可以把这样的东西放在哪里?

\setlength\leftmargini{0em}

有一些很好的例子在本文中,但那里的代码也经常不起作用。有一些很好的例子这里,但它们也不适合我想要的布局。

提前谢谢您!


一个工作示例(已更新):

\documentclass[a4paper, 14pt, titlepage]{article}

\usepackage{textcomp}
\usepackage{pifont}
\usepackage{xcolor}
\usepackage[american]{babel}

%my own description list
%newenvironment takes {description}[numberOfArguments]{before, processed before text in environment is processed}{after, see @ before} as definition
\definecolor{darkblue}{HTML}{0304A4}
\newenvironment{pathlist}{
    \renewcommand{\descriptionlabel}[1]{
        \parbox[b][2em][c]{\textwidth}{{\color{darkblue\textsf{\hspace{15px}##1}}}}
    \begin{description}
        \setlength{\rightmargin}{30px} % does not work at all
}{
    \end{description}
}

%response type expanding
\def\resptype#1{{\it \textbf{#1} (response type)}}

\begin{document}

\section{API}
Divided by HTTP methods.
\subsection{GET}
... some text \\
The text usually starts at the left edge, it will reach to the right edge     and everyone is happy. But why do the item bodies in the list below behave differently and why I can't set left- or rightmargin for them?

\begin{pathlist}
    \item[\texttt{nodes/\{id\}}]
    Gives a single node by its id.

    \item[\texttt{nodes/\{id\}/children}]
    Gives all children of a node, also if they are stored in a field, which is not called {\sf childIds}.
\end{pathlist}
\end{document}

答案1

enumitem这是一个使用和的简单解决方案url。我不确定是否完全理解了您的要求,但我认为这或多或少就是您想要的。

我定义了一种基于 的新列表类型,description并使用包进行了不同的格式设置urlbefore关键在于在输入列表 \ 之前执行的操作,此处缩短\linewidth。我简化了您的颜色代码,以便我们可以使用colors-by-namexcolor并且您的 HTML 颜色接近SVG's DarkBlue

除了注释之外,不要使用\sf, \it诸如此类的字体更改命令:命令plain TeX;请使用 NFSS\textsf, \textit\sffamily, \itshape。此外,标准类中不能有 14pt 的大小。为此请使用包extarticle中的类extsizes

\documentclass[a4paper, 14pt, titlepage]{extarticle}

\usepackage{textcomp}
\usepackage{pifont}
\usepackage[svgnames]{xcolor}
\usepackage[american]{babel}
\usepackage{enumitem}
%my own description list
%newenvironment takes {description}[numberOfArguments]{before, processed before text in environment is processed}{after, see @ before} as definition

\usepackage{url} %

\newlist{pathlist}{description}{1}
\setlist[pathlist, 1]{format = \color{DarkBlue}\mdseries\path, wide=15px, before={\addtolength\linewidth{-30px}}}
%response type expanding
\def\resptype#1{{\itshape\textbf{#1} (response type)}}
\DeclareUrlCommand\path{\urlstyle{sf}}

\begin{document}

\section{API}
Divided by HTTP methods.
\subsection{GET}
... some text \\
The text usually starts at the left edge, it will reach to the right edge and everyone is happy. But why do the item bodies in the list below behave differently and why I can't set left- or rightmargin for them?

\begin{pathlist}
  \item[\nodes/{id}]
  Gives a single node by its id.

  \item[{nodes/{id}/children}]
  Gives all children of a node, also if they are stored in a field, which is not called \textsf{childIds}.
\end{pathlist}
Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text.

\end{document} 

enter image description here

答案2

我又在 Google 上搜索了一下,发现列表类型\description对我来说太狭窄了(定义太明确)。像这样的命令\setlength{\leftmargin}{20pt}不会改变任何东西。现在我使用一些(它已经非常接近准备好了)的东西,例如:

\newenvironment{pathlist}{
    \let\olditem\item 
    \renewcommand\item[2]{\olditem \color{darkblue}##1\vspace*{2pt}\linebreak[4]
    \color{black}##2}
    \begin{list}{}{
        \setlength{\baselineskip}{1.3em}
        \setlength{\topsep}{5px}
        \setlength{\leftmargin}{10pt}
        \setlength{\rightmargin}{5pt}
    }
}{
    \end{list}
}

请注意,我没有使用标签的论点\list,但这也是可能的。在我的情况下,连同更新命令\label。引自latex2e文档:

强制标签参数指定应如何标记项目(除非为 \item 提供了可选参数)。此参数是插入框中以形成标签的一段文本。它可以并且通常包含其他 LA TEX 命令。

相关内容