# Command to get N equally spaced dots in mathmode

In mathmode, we have \cdots, \vdots, etc., to indicate repetitive patterns or ellipsis. These commands print three equally spaced dots.

Is there a command that achieves the same, only for an arbitrary number of N dots?

-
Does \newcommand*{\MyDots}[1]{\foreach \x in {1,...,#1} {\,.}} do what you desire? This requires \usepackage{tikz}. – Peter Grill Feb 7 '14 at 9:34
@PeterGrill well actually it only needs \usepackage{pgffor}, not all of tikz (but also see comments on this answer). – David Z Feb 7 '14 at 17:41

The example redefines \cdots to take an optional argument to specify the number of dots with default value 3:

\documentclass{article}

\makeatletter
\renewcommand*{\cdots}[1][3]{%
\mathinner{%
\expandafter\roman@repeat
\expandafter\cdotp
\romannumeral\numexpr(#1)\relax
000z%
}%
}
\newcommand*{\roman@repeat}[2]{%
\csname roman@repeat@#2\endcsname{#1}%
}
\newcommand*{\roman@repeat@m}[1]{%
#1%
\roman@repeat{#1}%
}
\newcommand*{\roman@repeat@z}[1]{}%
\makeatother

\begin{document}
$A \cdots[0] B \cdots[1] C \cdots[2] D \cdots D \cdots[4] E \cdots[5] F \cdots[6] G \cdots[7] H$
\end{document}


Note:

• Package amsmath redefines \cdots. It checks for following punctuation symbols. Then it is more robust to use a different command than \cdots for the macro that comes with an argument for the number of dots.

## Variant with \cleaders

Just for fun, a variant follows that uses \cleaders. Then a loop is not necessary for the TeX macro code.

\documentclass{article}

\makeatletter
\newcommand*{\cdotsx}[1]{%
\mathinner{%
}%
}
% #1: box contents if repeat count = 1
% #2: box contents of boxes after the first box
% #3: math style
% #4: repeat count
\ifnum\numexpr(#4)>\z@
\hbox{$#3#1\m@th$}%
\ifnum\numexpr(#4)>\@ne
\sbox0{$#3#2\m@th$}%
\fi
\fi
}
\makeatother

\begin{document}
$A \cdotsx{0} B \cdotsx{1} C \cdotsx{2} D \cdotsx{3} D \cdotsx{4} E \cdotsx{5} F \cdotsx{6} G \cdotsx{7} H$
\end{document}


-
You had my same idea. ;-) – egreg Feb 7 '14 at 9:55

I don't really know what you'd want these for, but here they are:

\documentclass{article}
\newcommand{\dodotx}[2]{%
\mathinner{%
\mathcodem=#1
\romannumeral\number#2 000
}%
}
\newcommand{\ldotx}[1]{\dodotx{\ldotp}{#1}}
\newcommand{\cdotx}[1]{\dodotx{\cdotp}{#1}}

\makeatletter
\newcommand{\vdotx}[1]{%
\vbox{
\baselineskip 4\p@
\lineskiplimit \z@
\kern 6\p@
\expandafter\dovdotx\romannumeral\number#1 000@
}%
}
\newcommand{\dovdotx}[1]{%
\if #1@\else\hbox{.}\expandafter\dovdotx\fi
}

\begin{document}
$\ldotx{2}\quad\cdotx{2}$

$\ldotx{3}\quad\cdotx{3}$

$\ldotx{4}\quad\cdotx{4}$

$\ldotx{5}\quad\cdotx{5}$

$\ldotx{6}\quad\cdotx{6}$

$\vdotx{3}\vdotx{4}\vdotx{5}$
\end{document}


Here's a LaTeX3 version; I added also \ddotx; the comparison with \ddots shows some difference, but diagonal dots are quite problematic in general.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\ldotx}{ m }
{
\ingo_dots_horizontal:Nn \ldotp { #1 }
}
\NewDocumentCommand{\cdotx}{ m }
{
\ingo_dots_horizontal:Nn \cdotp { #1 }
}
\NewDocumentCommand{\vdotx}{ m }
{
\ingo_dots_vertical:nn { 0 } { #1 }
}
\NewDocumentCommand{\ddotx}{ m }
{
\ingo_dots_vertical:nn { 4 } { #1 }
}

\cs_new:Npn \ingo_dots_horizontal:Nn #1 #2
{
\mathinner{ \prg_replicate:nn { #2 } { #1 } }
}

\int_new:N \l_ingo_reps_int
\cs_new_protected:Npn \ingo_dots_vertical:nn #1 #2
{
\vbox:n
{
\baselineskip 4pt
\lineskiplimit 0pt
\kern 6pt
\int_compare:nTF { #1 == 0 }
{
\prg_replicate:nn { #2 } { \hbox:n {.} }
}
{
\int_zero:N \l_ingo_reps_int
\prg_replicate:nn { #2 } { \ingo_shifted_dot:n { #1 } }
}
}
}

\cs_new_protected:Npn \ingo_shifted_dot:n #1
{
\hbox:n { \kern\dim_eval:n { #1pt * \l_ingo_reps_int } . }
\int_incr:N \l_ingo_reps_int
}

\ExplSyntaxOff

\begin{document}
$\ldotx{2}\quad\cdotx{2}$

$\ldotx{3}\quad\cdotx{3}$

$\ldotx{4}\quad\cdotx{4}$

$\ldotx{5}\quad\cdotx{5}$

$\ldotx{6}\quad\cdotx{6}$

$\vdotx{3}\vdotx{4}\vdotx{5}\ddotx{5}$

$\ddotx{3}\ddots$
\end{document}


-
How do you decide (in LaTeX3) whether you use \cs_new_protected:Npn or \cs_new:Npn? – Manuel Feb 7 '14 at 10:38
Use the protected variety when the function makes assignments. It's not the only requirement; in general, use \cs_new:Npn when the function is "safe" in full expansions contexts. – egreg Feb 7 '14 at 10:42
Is a rationale really always necessary ;-)? Great answer though, thanks a lot. – Ingo Feb 13 '14 at 9:36

Using the definitions of \cdots and \vdots from fontmath.ltx and modifying them a bit (requires pgffor) I've defined three commands \nldots, \ncdots, and \nvdots which take an optional argument with default value 3.

\documentclass[a4paper,12pt]{book}
\usepackage{pgffor}

\makeatletter
\DeclareRobustCommand
\nldots[1][3]{\mathinner{\foreach \x in {1,...,#1}{\ldotp}}}
\DeclareRobustCommand
\ncdots[1][3]{\mathinner{\foreach \x in {1,...,#1}{\cdotp}}}
\DeclareRobustCommand
\nvdots[1][3]{\vbox{\baselineskip4\p@ \lineskiplimit\z@
\kern6\p@\foreach \x in {1,...,#1}{\hbox{.}}}}
\makeatother

\begin{document}

$A\nldots[5] B\ldots C$
$A\ncdots[5] B\cdots C$
$\nvdots[5] \nvdots \vdots$

\end{document}


Output:

-
you can use pgffor as a package to use \foreach. But there is a bug that is fixed in the coming version that you need to include pgf` in order to make it work in the meantime :) – percusse Feb 7 '14 at 10:08
@percusse Yes, thanks. – karlkoeller Feb 7 '14 at 10:09