5
\documentclass{article}
\usepackage{enumitem}
\begin{document}

\begin{enumerate}[align=parleft]
  \item[2.1] An example \dotfill 21
  \item[2.2] A longer example like this such that the line wraps down to the next line and the last entry gets wrapped to the next line \dotfill 22
  \item[2.31] Another example \dotfill 132
\end{enumerate}

\end{document}

I use '\dotfill', but the dots are aligned at the bottom of the text.

enter image description here

2
  • Are you trying to make a table of contents by hand?
    – egreg
    Sep 22, 2017 at 17:31
  • @egreg I am trying to make a table of contents about the defined terms in my book.(with '\cdotfill' in jaytar's answer , '\ref', '\pageref')
    – K.Robert
    Sep 22, 2017 at 17:42

2 Answers 2

9

I use a \cdotfill command for this:

\documentclass{article}
\usepackage{enumitem}
\makeatletter
\newcommand\cdotfill{%
    \leavevmode\cleaders\hb@[email protected]{\hss$\cdot$\hss}\hfill\kern\z@
}
\makeatother
\begin{document}
\begin{enumerate}[align=parleft]
  \item[2.1] An example \cdotfill 21
  \item[2.2] A longer example like this such that the line wraps down to the next line and the last entry gets wrapped to the next line \ddotfill 22
  \item[2.31] Another example \ddotfill 132
\end{enumerate}

\end{document}

Output:

output

Is this what you want?

Edit: To explain the command a bit, so you can play around with it:

  • \leavevmode ensures that horizontal mode is enabled, so it doesn't stack boxes vertically ("leave vertical mode")
  • \cleaders indicates that the following statement will be repeated until a certain skip is reached
  • \hb@xt@ is an TeX primitive abbreviation for \hbox to and defines a horizontal box of the given length (in this case .44em)
  • {\hss$\cdot$\hss} is the object inside the hbox, where \hss is a horizontal space that can take any length
  • \hfill is the second argument of \cleaders. It says "fill the entire left space" and since it is used in the context of \cleaders, the entire left space is repeatedly filled with the box.
  • \kern\z@ determines the space added before the number, where \z@ is a TeX primitive for 0pt (so currently no space is added and \kern just means horizontal space of fixed width.
7
  • yes,exactly! i am looking for a command named cdotfill…
    – K.Robert
    Sep 22, 2017 at 17:08
  • The command \cdotfill is defined in my answer.
    – jaytar
    Sep 22, 2017 at 17:09
  • I know, I mean I am looking for how to define a command similar to \cdotfill.
    – K.Robert
    Sep 22, 2017 at 17:31
  • What do you want to change? You can change the pattern by replacing $\cdot$ and the spacing by replacing .44em with any length you want. If can't help you if I don't know what you want.
    – jaytar
    Sep 22, 2017 at 18:10
  • maybe there is a mistake between us... I mean the answer you have given is exactly what I want. Maybe I accidentally double-click the accept button...
    – K.Robert
    Sep 22, 2017 at 18:17
3

Building on Jaytar’s fine answer, you may want to avoid line breaks at the right margin.

However, all leader dots I've seen are on the baseline. With this macro based approach, you can change the leaders by just changing the definition of \toclikeentry.

\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}

\usepackage{showframe} % just for the example

\let\cdotfill\dotfill
\patchcmd{\cdotfill}
  {\hss.\hss}
  {\hss\textperiodcentered\hss}
  {}{}
\newcommand{\changerightskip}{%
  \setlength{\rightskip}{3em}%
  \setlength{\parfillskip}{-3em}%
}
\newlist{toclikeI}{enumerate}{1}
\setlist[toclikeI]{align=parleft}
\newenvironment{toclike}{\toclikeI\changerightskip}{\endtoclikeI}
\newcommand{\toclikeentry}[3]{\item[#1]#2\cdotfill#3}

\begin{document}

\begin{toclike}%\changerightskip

\toclikeentry{2.1}{An example}{21}

\toclikeentry{2.2}{%
  A longer example like this such that the line
  wraps down to the next line and the last entry
  gets wrapped to the next line}{22}

\toclikeentry{2.31}{Another example}{132}

\end{toclike}

\end{document}

enter image description here

2
  • How could I get the numbers 2.1, 2.2 and 2.31 right-aligned as in a regular enumerate environment?
    – Tonechas
    Mar 12, 2023 at 8:36
  • 1
    @Tonechas Remove \setlist[toclikeI]{align=parleft}
    – egreg
    Mar 12, 2023 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .