如何用 tikzmark 绘制这些线条

如何用 tikzmark 绘制这些线条

我想画线来说明包装使用的长度enumitem,所以我创建了这个 MWE

\documentclass{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\usepackage{enumitem}
\setlength{\parindent}{0pt}
\begin{document}
\quad

\vfill
\begin{enumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{enumerate}
\vfill

\begin{tikzpicture}[remember picture, overlay]
\coordinate (left) at ($(current page.west)+(1.5,0)$);

\draw[red]($(left)+(0,-1)$)--($(left)+(2,-1)$)%
   node[midway ,anchor=north]{\tiny leftmargin};
\draw[blue]($(left)+(3,-1.3)$)--($(left)+(2,-1.3)$)%
   node[midway,anchor=north]{\tiny itemindent};

\draw[red]($(left)+(0,0.5)$)--($(left)+(1,0.5)$)%
   node[midway,anchor=south]{\tiny labelindent};
\draw[blue]($(left)+(1,1)$)--($(left)+(1.5,1)$)%
   node[midway,anchor=south ]{\tiny labelwidth};
\draw[red]($(left)+(1.5,0.5)$)--($(left)+(3,0.5)$)%
   node[midway,anchor=south]{\tiny labelsep};
\end{tikzpicture}

\end{document}

我们得到了

在此处输入图片描述

但这只有当我们将列表放在页面中间时才有效,我该如何改进此代码以tikzmark使行随列表移动,谢谢。

答案1

这是另一个选项,使用tikzmark图书馆和tikzpagenodes包;长度将根据enumitem所使用的设置自动调整并显示当前值:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc,tikzmark}
\usepackage{enumitem}

\def\showlabels#1#2{%
\begin{tikzpicture}[remember picture,overlay]
\draw[red] 
  (current page text area.west|-{pic cs:#2}) --
    node[below,font=\scriptsize] {leftmargin=\the\leftmargin} 
  ++(\leftmargin,0);
\draw[blue] 
  ([xshift=\leftmargin,yshift=-\baselineskip]current page text area.west|-{pic cs:#2}) --
    node[below,font=\scriptsize] {itemindent=\the\itemindent} 
  ([yshift=-\baselineskip]{{pic cs:#1}|-{pic cs:#2}});
\draw[red] 
  ([yshift=3\baselineskip]current page text area.west|-{pic cs:#1}) --
    node[above,font=\scriptsize] {labelindent=\the\labelindent} 
  ++(\labelindent,0);
\draw[blue] 
  ([xshift=\labelindent,yshift=1.5\baselineskip]current page text area.west|-{pic cs:#1}) --
    node[above,font=\scriptsize] {labelwidth=\the\labelwidth}
  ++(\labelwidth,0);
\draw[red] 
  ([xshift=\the\dimexpr\labelindent+\labelwidth\relax,yshift=0.5\baselineskip]current page text area.west|-{pic cs:#1}) --
    node[above,font=\scriptsize] {labelsep=\the\labelsep} 
  ([yshift=0.5\baselineskip]pic cs:#1);
\end{tikzpicture}%
}

\begin{document}

\begin{enumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm,align=left]
\item\tikzmark{a}text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text\tikzmark{b}
\showlabels{a}{b} 
\end{enumerate}

\vskip3cm

\begin{enumerate}[label=\arabic* -,labelwidth=60pt,leftmargin=4cm,labelsep=30pt]
\item\tikzmark{c}text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text\tikzmark{d}
\showlabels{c}{d} 
\end{enumerate}

\vskip3cm

\begin{enumerate}
\item\tikzmark{e}text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text\tikzmark{f}
\showlabels{e}{f} 
\end{enumerate}

\end{document}

\item只需在using后立即放置一个标记\tikzmark{<name1>},并在 using 项的最后一个单词后立即放置另一个标记\tikzmark{<name2>},然后使用\showlabels{<name1>}{<name2>}。代码需要运行三次才能稳定。

答案2

这里,我没有使用覆盖,而是将各tikz部分存储在两个宏(enummetricsA\enummetricsZ)中,调用labelenumerate宏时会自动调用这两个宏。由于底部的“leftindent”标签如果相对于环境顶部固定,则会进入文本,因此我将其相对于环境底部固定。

\documentclass{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\usepackage{enumitem}
\setlength{\parindent}{0pt}
\def\enummetricsA{
\begin{tikzpicture}
\coordinate (left) at ($(current page.west)+(1.5,0)$);
\draw[red]($(left)+(0,0.5)$)--($(left)+(1,0.5)$)%
   node[midway,anchor=south]{\tiny labelindent};
\draw[blue]($(left)+(1,1)$)--($(left)+(1.5,1)$)%
   node[midway,anchor=south ]{\tiny labelwidth};
\draw[red]($(left)+(1.5,0.5)$)--($(left)+(3,0.5)$)%
   node[midway,anchor=south]{\tiny labelsep};
\end{tikzpicture}}
\def\enummetricsZ{
\begin{tikzpicture}[remember picture]
\coordinate (left) at ($(current page.west)+(1.5,0)$);
\draw[red]($(left)+(0,-1)$)--($(left)+(2,-1)$)%
   node[midway ,anchor=north]{\tiny leftmargin};
\draw[blue]($(left)+(3,-1.3)$)--($(left)+(2,-1.3)$)%
   node[midway,anchor=north]{\tiny itemindent};
\end{tikzpicture}}

\newenvironment{labelenumerate}
{\noindent\kern-2.1ex\smash{\raisebox{-1.1\baselineskip}{\enummetricsA}}\begin{enumerate}}
{\end{enumerate}\noindent\kern-0.9ex\smash{\raisebox{-.0\baselineskip}{\enummetricsZ}}}

\begin{document}
\quad

\bigskip
\begin{enumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{enumerate}%

\begin{enumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{enumerate}%

\begin{labelenumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{labelenumerate}%

\begin{enumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{enumerate}%

\begin{labelenumerate}[label=\arabic* -, leftmargin=2cm, labelsep= 1.5cm,itemindent=1cm]
\item text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
\end{labelenumerate}%

\end{document}

在此处输入图片描述

相关内容