description 列表环境如何实现换行、对齐和打印代码?

发布于 2025-11-30 21:20:23

想要复刻 asymptote 手册中的列表环境(不确定是不是列表环境)

  • 操作系统:Win11
  • 发行版:TeXLive2025
  • 编辑器TeXStudio
  • 编译命令
  • 示例代码list_test.tex

注:

  1. typelist funclist 是刘海洋老师在 asy4cn/Asymptote-zh 的一个实现,(貌似有点复杂?能不能使用 enumitem 或者类似简单的方案实现呢?)
  2. 其他的是我用 enumitem tabularray 都不太方便(tabularray,每次都要写两行,而且没有办法使用minted 环境列举示例)或者没有达到想要的效果(enumitem)
  3. 如何实现在列表项中,也就是 \item[<label>] <description> 中使用 mintedinline 命令?如果是 \verb 的话,是可以使用 fancyvrb 宏包提供的 \Verb 代替的,一个简单的例子如下:
%\usepackage{fancyvrb}
\begin{description}
\item[\Verb|something|]
\end{description}

第一种列表
image.png
第二种列表
image.png
完整的效果应该如下
image.png
此外在 tex.stackexchange/description-list-how-to-put-the-definition-on-a-new-line 记录这使用 \hfill\\ 手动换行的方法,在 latexstudio/355. LaTeX技巧303:description列表不得不说的用法 上也有记载。
完整的一个代码?

\documentclass[fontset=fandol]{ctexart} \pagestyle{empty}
\usepackage{geometry}
\geometry{left=2.5cm,right=2.5cm,top=3.5cm,bottom=2.5cm}
\ctexset{
    section/format=\Large\bfseries\raggedright\color{red!60!black}
}

\usepackage{xcolor}

\newenvironment{funclist}{\trivlist
  \parindent=0pt
\item[]
  \def\item{\medskip\par\leftskip=0pt}
  \def\go{\par\leftskip=4em}}
{\endtrivlist}

\makeatletter
\newenvironment{typelist}{\itemize
  \let\old@item\@item
  \def\@item[##1]{\expandafter\old@item[\ttfamily##1]}}
{\enditemize}
\makeatother

\usepackage{enumitem}

\usepackage{tabularray}
\usepackage{minted}
\usepackage{codehigh}
\usepackage{fancyvrb}
\begin{document}
\begin{description}
\item[\Verb|something|] 
\end{description}
\section{typelist}
\begin{typelist}
\item[void] 空(\verb|void|)类型仅用于无参数或无返回值的函数。
\item[bool] 布尔类型只能取值
\end{typelist}

\section{funclist}
\begin{funclist}
\item Shift \go
  仅随嵌入它的图平移;
\item Rotate \go
  仅随嵌入它的图平移和旋转(默认值);
\item Rotate(pair z) \go
  按(随图变换过的)向量 z 旋转;
\end{funclist}

\section{description}
\begin{description}[labelsep*=2em,font=\ttfamily,labelwidth=6em,align=parleft]
\item [Shift] 仅随嵌入它的图平移;
\item [Rotate]  仅随嵌入它的图平移和旋转(默认值);
\item [Rotate(pair z)] 按(随图变换过的)向量 z 旋转;
\item [Rotate(pair\;z)] 按(随图变换过的)向量 z 旋转;
\end{description}

\section{description-nextline}
\begin{description}[style=nextline,font=\ttfamily,labelwidth=!,align=parleft]
\item [Shift] 仅随嵌入它的图平移;
\item [Shift] 仅随嵌入它的图平移;仅随嵌入它的图平移;
\item [Rotate(pair z)] 按(随图变换过的)向量 z 旋转;
\item [Rotate(pair\;z)] 按(随图变换过的)向量 z 旋转;
\end{description}

\section{tabularray-mintinline}
\begin{table}[H]
\raggedright
\begin{tblr}{
    colspec={Q[l,4em]Q[l]},
%    column{1}={font=\ttfamily},
    cell{odd}{1}={r=1,c=2}{
        font=\ttfamily,
        halign=l,
        preto=\mintinline{asy}|,
        appto=|
    }
}
\mintinline{asy}|Shift| & \\
& 仅随嵌入它的图平移;\\
Shift & \\
& 仅随嵌入它的图平移;仅随嵌入它的图平移;\\
Rotate(pair z) & \\
 & 按(随图变换过的)向量 z 旋转;\\
\fakeverb{Shift} & \\
& 仅随嵌入它的图平移;\\
Shift & \\
& 仅随嵌入它的图平移;仅随嵌入它的图平移;\\
Rotate(pair z) & \\
& 按(随图变换过的)向量 z 旋转;\\
\& & \\
& 按(随图变换过的)向量 z 旋转;\\
\end{tblr}
\end{table}

\section{tabularray-fakeverb}
\begin{table}[H]
\raggedright
\begin{tblr}{
    colspec={Q[l,4em]Q[l]},
%    column{1}={font=\ttfamily},
    cell{odd}{1}={r=1,c=2}{
        font=\ttfamily,
        halign=l,
        cmd=\fakeverb
    }
}
Shift & \\
& 仅随嵌入它的图平移;\\
Shift & \\
& 仅随嵌入它的图平移;仅随嵌入它的图平移;\\
Rotate(pair z) & \\
 & 按(随图变换过的)向量 z 旋转;\\
\fakeverb{Shift} & \\
& 仅随嵌入它的图平移;\\
Shift & \\
& 仅随嵌入它的图平移;仅随嵌入它的图平移;\\
Rotate(pair z) & \\
& 按(随图变换过的)向量 z 旋转;\\
\& & \\
& 按(随图变换过的)向量 z 旋转;\\
\end{tblr}
\end{table}
\end{document}

查看更多

关注者
0
被浏览
116
1 个回答
雾月
雾月 7小时前
这家伙很懒,什么也没写!

不是已经实现了吗,没懂哪里有问题,除了 label 单独一行外,也没懂一二种列表有什么区别。

enumitem 的实现,需要用 verb 的地方,在合适的地方用 \scantokens 重新扫描一下就行了(\sitem 的实现方式):

\NewDocumentCommand\sitem{+v}{\item[\scantokens{#1}]}
\newlist{mydesc}{description}{2}
\setlist[mydesc,1]{}
\setlist[mydesc,2]{style=nextline}

\begin{mydesc}
\sitem{\Verb|something|} 劳仑
\sitem{\mintinline{asy}|Rotate(pair z)|} 普桑
  \begin{mydesc}
    \sitem{wa hh} 劳仑\\ 衣
    % \item[wa hh] 也可以用
    \sitem=\Verb|\{}|= 普桑
  \end{mydesc}
\end{mydesc}

label 宽度可以自己改改。
image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览