通过计数器的倍数来确定间距

通过计数器的倍数来确定间距

我有\hspace,我有一个stepcounterxyz

\documentclass[12pt]{article}
\usepackage[top = 1in, bottom = 1in, left = 1in, right = 1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}

\newcounter{xyz} \stepcounter{xyz}

\newcommand*\xyz{\stepcounter{xyz} \hspace*{-6.1em} 
   \hangindent=9mm \noindent 
   \textbf{\textit{\arabic{xyz}}} 
   \hspace{2.1em}}

\begin{document}

\xyz 
cbvcb cb vc bcvb cvbb c cbcvb cvbvbcvbvbv  c bvcbcvbvcbvbcbvccb cb vcb cvb cvb cv cbc vvbvbb b cvb cvb cb vc bcvbcbvvcv bcbvcbcb cb cv cb cvbcbvbcbdfgfgcbvb cvb cb cvb cb cb vcb c c bv cb cbcb  cbvc cb vb c cvb cvb

\end{document}

如何使和中的 A 和 B\hspace*{-A mm}成为\hspace{B mm}字符串长度(以毫米为单位)的倍数\textbf{\textit{\arabic{xyz}}}?如何定义此类变量以及如何将它们输入\hspace

编辑:我该如何减去或添加 Z mm,如\hspace*{2\mywid + Z}\hspace*{2\mywid - Z}?在这种情况下,加号和减号不起作用。

答案1

您必须使用长度变量。然后,允许使用其倍数。

在这里,我使左缩进相同,并根据计数器值对右缩进进行索引:

根据 OP 的请求,已编辑以允许缩进进行“+/-Z”偏移。

\documentclass[12pt]{article}
\usepackage[top = 1in, bottom = 1in, left = 1in, right = 1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{calc}
\newcounter{xyz} 
\newlength\mywid
\newlength\myshift
\newlength\z
\setlength\z{3mm}
\setlength\mywid{\widthof{\textbf{\textit{\arabic{xyz}}}}}

\newcommand*\xyz{\stepcounter{xyz} 
   \setlength\myshift{-2\mywid + \z}
   \hspace*{\myshift} 
   \hangindent=9mm \noindent 
   \textbf{\textit{\arabic{xyz}}} 
   \setlength\myshift{\thexyz\mywid - \z}
   \hspace{\myshift}
}
\begin{document}

\xyz 
cbvcb cb vc bcvb cvbb c cbcvb cvbvbcvbvbv  c bvcbcvbvcbvbcbvccb cb vcb cvb cvb cv cbc vvbvbb b cvb cvb cb vc bcvbcbvvcv bcbvcbcb cb cv cb cvbcbvbcbdfgfgcbvb cvb cb cvb cb cb vcb c c bv cb cbcb  cbvc cb vb c cvb cvb

\xyz 
cbvcb cb vc bcvb cvbb c cbcvb cvbvbcvbvbv  c bvcbcvbvcbvbcbvccb cb vcb cvb cvb cv cbc vvbvbb b cvb cvb cb vc bcvbcbvvcv bcbvcbcb cb cv cb cvbcbvbcbdfgfgcbvb cvb cb cvb cb cb vcb c c bv cb cbcb  cbvc cb vb c cvb cvb

\xyz 
cbvcb cb vc bcvb cvbb c cbcvb cvbvbcvbvbv  c bvcbcvbvcbvbcbvccb cb vcb cvb cvb cv cbc vvbvbb b cvb cvb cb vc bcvbcbvvcv bcbvcbcb cb cv cb cvbcbvbcbdfgfgcbvb cvb cb cvb cb cb vcb c c bv cb cbcb  cbvc cb vb c cvb cvb

\end{document}

在此处输入图片描述

答案2

与 Steven 的解决方案非常相似,但没有使用该calc包:

\documentclass[12pt]{article}
\usepackage[top = 1in, bottom = 1in, left = 1in, right = 1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}

\newcounter{xyz}

\newlength{\mylength}

\newcommand*\xyz{%
   \stepcounter{xyz}%
   \settowidth{\mylength}{\textbf{\textit{\arabic{xyz}}}}%
   \hspace*{-3\mylength}%
   \hangindent=9mm \noindent%
   \textbf{\textit{\arabic{xyz}}}%
   \hspace{2\mylength}%
   }

\begin{document}

\xyz
cbvcb cb vc bcvb cvbb c cbcvb cvbvbcvbvbv  c bvcbcvbvcbvbcbvccb cb vcb cvb cvb cv cbc vvbvbb b cvb cvb cb vc bcvbcbvvcv bcbvcbcb cb cv cb cvbcbvbcbdfgfgcbvb cvb cb cvb cb cb vcb c c bv cb cbcb  cbvc cb vb c cvb cvb

\end{document} 

在此处输入图片描述

相关内容