Coming from something like this:

\documentclass{memoir}

\begin{document}
\tableofcontents*
\newpage
\chapter{A chapter}
\section{A section}
\section{A longer named section}
\newpage
\chapter{Another chapter}
\section{A section}
\section{Yet another section}
\section{And a really long section named which probably is so very long that
some people might think is too long}
\end{document}


Can I have something that looks more like what is below here?

                        Contents

1  A chapter                 3
1.1            A section 3
1.2       A longer named 3
section

2  Another chapter           5
2.1            A section 5
2.2  Yet another section 5
2.3    And a really long 5
section named which
probably is so very
long that some
people might think
it is too long


So, instead of filling out with dots the names are flushed right, next to the page numbers.

If you have subsections, etc.., this will have to be completed by appropriate additional definitions. Also, for compatibility with memoir one needs a version of etoc either more recent or more ancient than 1.07k.

\documentclass{memoir}
% goal:

%                         Contents

%            1  A chapter                 3
%                1.1            A section 3
%                1.2       A longer named 3
%                                 section

%            2  Another chapter           5
%                2.1            A section 5
%                2.2  Yet another section 5
%                2.3    And a really long 5
%                     section named which
%                     probably is so very
%                          long that some
%                      people might think
%                          it is too long

\usepackage{etoc}[2014/04/22]% 1.07l (or < 1.07k) needed for memoir compatibility

% these things being used either in bold or medium series contexts, better not
% to use em or ex for them.
\newcommand{\marginC}{15pt}% reserved space for chapter numbers
\newcommand{\marginS}{20pt}% reserved space for section numbers
\newcommand{\marginP}{15pt}% reserved space for page numbers

\newlength{\oldleftskip}

\etocsettocstyle{\leftskip .2\linewidth% to be customized
\rightskip.2\linewidth
\parindent   0pt\relax
\parfillskip 0pt plus 1fil\relax
\markboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}%
\noindent\hfil\Large\bfseries\contentsname\par
%
\normalsize\mdseries\bigskip
}{}

\etocsetstyle{chapter}
{}
{\parfillskip-\marginP\relax
\bfseries
\noindent\llap{\makebox[\marginC][l]{\etocnumber}}%
\etocname\nobreak\hfill\makebox[\marginP][r]{\etocpage}\par
\smallskip}
{}{}

\etocsetstyle{section}
{\mdseries}
{\leavevmode
\oldleftskip\leftskip
\parfillskip-\marginP\relax
\makebox[\marginS][l]{\etocnumber}%
\hfill\makebox[\marginP][r]{\etocpage}\par\nobreak
\vspace{-\baselineskip}
\leftskip \the\dimexpr\oldleftskip+\marginS\relax plus 1fil\relax
\parfillskip 0pt\relax
\etocname\par
\leftskip\oldleftskip}
{}{\medskip}

\begin{document}
\tableofcontents*
\newpage
\chapter{A chapter}
\section{And a really long named section which probably is so very long that
some people might think is too long}
\section{A section}
\section{A longer named section}
\newpage
\chapter{Another chapter which has a long, very very long name}
\section{A section}
\section{Yet another section}
\section{And a really long named section which probably is so very long that
some people might think is too long}
\end{document}

• Wow, this is impressive work. I realize now that I see your example that maybe the page number reference for chapter level should be on the first line as well. I must take some time real soon now and try to decipher all this code! – jonalv May 7 '14 at 13:24
• I somehow naïvely thought this couldn't be so difficult... – jonalv May 7 '14 at 13:44
• @jonalv having at the same time the page number on the first line and the text flushed right was not easy indeed, but perhaps a simpler way exists. I avoid using \parboxes or minipages to allow the table of contents to have page breaks at arbitrary locations (meaning: within very long names; but one should add \nobreak to avoid a page break after a chapter name -- a problem might arise with a very long first section name immediately following a chapter, though) . Else, it would be much easier. – user4686 May 7 '14 at 15:58