一元枚举

一元枚举

我想要一份enumerate清单,用一元表示的数字也就是说,“1”将表示为,“3”将表示为•••,“8”将表示为•••••|•••

我已经能够打印点了,而且工作正常:

\newcommand{\xxdot}{\textbullet} % The dot character to use
\newcounter{loopcntr} % A one-off counter for \forloop

\newcommand{\xxdots}[1]{% Print #1 dots in a row
    \ifthenelse{#1>5}{% If there are more than five, break them into groups
        \xxdots{5}\textpipe\xxdots{\number\numexpr#1-5\relax}% By printing five, then a pipe, then the rest
    }{% Otherwise, just print that many dots
        \forloop{loopcntr}{0}{\value{loopcntr}<#1}{\xxdot}%
    }%
}
\newcommand{\xdots}[1]{\textnormal{\xxdots{#1}}} % Wrap it in textnormal style

但是,我不知道如何利用enumerate这一点。基于这个答案,我尝试重新定义\theenumi

\renewcommand\theenumi{\xdots{\arabic{enumi}}}

但事情却大错特错:

! Illegal parameter number in definition of \@currentlabel.
<to be read again> 
                   1
l.376 ^^I\item My text here ...
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

我如何将我的一元打印代码应用到enumerate标签上?

编辑:基于这个答案,我尝试了这个:

\newcommand*\unary[1]{\expandafter\@unary\csname c@#1\endcsname}
\newcommand*\@unary[1]{\xdots{\the\numexpr#1\relax}}

看起来成功了!如下所示,它产生了正确的输出:

\newcounter{test}
\setcounter{test}{8}
\unary{test}

然而,MWE 仍然会抛出一堆错误。

MWE 如下:

\documentclass{article}

\usepackage{forloop}
\usepackage{tipa}
\usepackage[T1]{fontenc}

\newcommand{\xxdot}{\textbullet} % The dot character to use
\newcounter{loopcntr} % A one-off counter for \forloop

\newcommand{\xxdots}[1]{% Print #1 dots in a row
    \ifthenelse{#1>5}{% If there are more than five, break them into groups
        \xxdots{5}\textpipe\xxdots{\number\numexpr#1-5\relax}% By printing five, then a pipe, then the rest
    }{% Otherwise, just print that many dots
        \forloop{loopcntr}{0}{\value{loopcntr}<#1}{\xxdot}%
    }%
}
\newcommand{\xdots}[1]{\textnormal{\xxdots{#1}}} % Wrap it in textnormal style

\makeatletter
\newcommand*\unary[1]{\expandafter\@unary\csname c@#1\endcsname}
\newcommand*\@unary[1]{\xdots{\the\numexpr#1\relax}}
\makeatother

%\renewcommand\theenumi{\unary{enumi}} % This crashes and burns

\begin{document}

Unary works normally: \xdots{1}, \xdots{3}, \xdots{8}.

It even works with a counter.

\newcounter{test}
\setcounter{test}{8}
\unary{test}

But in a list:

\begin{enumerate}
    \item My text here my text here
    \item More text more text
    \item Even more text
\end{enumerate}

\end{document}

答案1

enumitem包是您的朋友。您可以定义一个新的列表样式(我称之为)unerate,然后让此列表样式使用您的\unary代码。要实现这一点,只需将以下几行添加到您的代码中即可:

\let\realItem\item
\newcommand\unerateItem{\refstepcounter{uneratei}\realItem[\unary{uneratei}]}
\usepackage{enumitem}
\newlist{unerate}{enumerate}{1}
\setlist[unerate]{label=\alph*, before=\let\item\unerateItem }

因此,这将重新定义\item\unerateItem,而这又将“实数”\item应用于\unary列表环境计数器,该计数器称为uneratei。有了这个,代码

\begin{unerate}
    \item My text here my text here
    \item More text more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
\end{unerate}

生成:

在此处输入图片描述

完整代码如下:

\documentclass{article}

\usepackage{forloop}
\usepackage{tipa}
\usepackage[T1]{fontenc}

\newcommand{\xxdot}{\textbullet} % The dot character to use
\newcounter{loopcntr} % A one-off counter for \forloop

\newcommand{\xxdots}[1]{% Print #1 dots in a row
    \ifthenelse{#1>5}{% If there are more than five, break them into groups
        \xxdots{5}\textpipe\xxdots{\number\numexpr#1-5\relax}% By printing five, then a pipe, then the rest
    }{% Otherwise, just print that many dots
        \forloop{loopcntr}{0}{\value{loopcntr}<#1}{\xxdot}%
    }%
}
\newcommand{\xdots}[1]{\textnormal{\xxdots{#1}}} % Wrap it in textnormal style

\makeatletter
\newcommand*\unary[1]{\expandafter\@unary\csname c@#1\endcsname}
\newcommand*\@unary[1]{\xdots{\the\numexpr#1\relax}}
\makeatother

\let\realItem\item
\newcommand\unerateItem{\refstepcounter{uneratei}\realItem[\unary{uneratei}]}
\usepackage{enumitem}
\newlist{unerate}{enumerate}{1}
\setlist[unerate]{label=\alph*, before=\let\item\unerateItem }

\begin{document}

  \begin{unerate}
    \item My text here my text here
    \item More text more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
    \item Even more text
  \end{unerate}

\end{document}

您现在可以随心所欲enumitem地设计这些环境的风格。

上面的代码不允许您嵌套unerate环境。如果需要,请告诉我,我会更新它 - 这并不难,但需要一些额外的工作。

答案2

我回收了此代码并稍微延伸一下得到

\documentclass{article}
\usepackage{pgffor}
\newcommand{\nbullets}[2][\textbullet]{%
\foreach \X [evaluate=\X as \Y using {int(mod(\X,5))}]in {1,...,#2}
{\ifnum\X>1%
\ifnum\Y=1%
$\vert$\fi%
\fi%
#1}}
\begin{document}
\renewcommand{\labelenumi}{\nbullets{\value{enumi}}}
\begin{enumerate}
 \item first
 \item second
 \item third
 \item fourth
 \item fifth
 \item sixth
 \item seventh
 \item last 
\end{enumerate}

Nestable:
\renewcommand{\labelenumii}{\nbullets[*]{\value{enumii}}}
\begin{enumerate}
 \item first
 \item second
 \begin{enumerate}
  \item a
  \item b
  \item c
  \item d
  \item e
  \item f
 \end{enumerate}
 \item third
 \item fourth
 \item fifth
 \item sixth
 \item seventh
 \item last 
\end{enumerate}
\end{document}

在此处输入图片描述

Stefan 的版本。(\TCS基本上来自这里

\documentclass{article}
\usepackage{tikz}
\newcommand{\nbullets}[2][\textbullet]{%
\foreach \X [evaluate=\X as \Y using {int(mod(\X,5))}]in {1,...,#2}
{\ifnum\X>1%
\ifnum\Y=1%
$\vert$\fi%
\fi%
#1}}
\newcommand{\TCS}[2][]{\begin{tikzpicture}[baseline,#1]
\foreach \X [evaluate=\X as \Y using {int(mod(\X,5))}]in {1,...,#2}
{\ifnum\Y=0
\draw (\X*0.5ex+0.3ex,0) -- ++(-2.8ex,2ex);
\else
\draw (\X*0.5ex+0.3ex,0) -- ++(0,2ex);
\fi}
\end{tikzpicture}}
\begin{document}
\renewcommand{\labelenumi}{\TCS{\value{enumi}}}
\renewcommand{\labelenumii}{\nbullets{\value{enumii}}}
\begin{enumerate}
 \item first
 \item second
 \begin{enumerate}
  \item a
  \item b
  \item c
  \item d
  \item e
  \item f
 \end{enumerate}
 \item third
 \item fourth
 \item fifth
 \item sixth
 \item seventh
 \item last 
\end{enumerate}
\end{document}

在此处输入图片描述

答案3

这是一个可扩展的\unifive宏:

\documentclass{article}
\usepackage{xparse,enumitem,tipa}

\usepackage{showframe}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\unifive}{m}
 {% #1 should be an integer
  \draconis_unifive:n { #1 }
 }

\cs_new:Nn \draconis_unifive:n
 {
  \int_compare:nTF { #1 > 5 }
   {
    \unifivefive
    \draconis_unifive:e { \int_eval:n { #1 - 5 } }
   }
   {
    \prg_replicate:nn { #1 } { \unifiveone }
   }
 }
\cs_generate_variant:Nn \draconis_unifive:n { e }

\NewDocumentCommand{\unifivefive}{}
 {
  \unifiveone\unifiveone\unifiveone
  \unifiveone\unifiveone\unifivebar
 }

\NewDocumentCommand{\unifiveone}{}{\textbullet}
\NewDocumentCommand{\unifivebar}{}{\textpipe}

\ExplSyntaxOff

\makeatletter
\newcommand{\unifivecount}[1]{\expandafter\@unifivecount\csname c@#1\endcsname}
\newcommand{\@unifivecount}[1]{\unifive{#1}}
\AddEnumerateCounter{\unifivecount}{\@unifivecount}{\unifive{10}}
\makeatother

\newlist{unerate}{enumerate}{1}
\setlist[unerate]{label=\unifivecount*,leftmargin=*}

\begin{document}

\begin{unerate}
  \item My text here my text here
  \item More text more text
  \item Even more text
  \item Even more text
  \item Even more text
  \item Even more text
  \item Even more text
  \item Even more text
  \item Even more text
  \item Even more text
\end{unerate}

\unifive{42}

\end{document}

在此处输入图片描述

答案4

你不需要显式循环来生成多个项目符号

在此处输入图片描述

\documentclass{article}

\renewcommand\labelenumi{\theenumi\space}
\renewcommand\theenumi{\expandafter\zzm\romannumeral\the\value{enumi}000\relax}

\def\zzm#1{\if m#1\textbullet\expandafter\zzm\fi}

\begin{document}

\begin{enumerate}
    \item My text here my text here
    \item More text more text
    \item Even more text
\end{enumerate}

\end{document}

相关内容