2

This code generates the following output:

\documentclass{memoir}
\usepackage{lipsum}
\usepackage{titletoc}
\usepackage{tikz}

\titlecontents{psection}[2.3em] {\large} {\contentslabel{2.5em}} {} {\hspace*{.5em}\tikz[baseline]{\node[draw, inner sep=3pt, rounded corners, fill=red!30, anchor=base]{\thecontentspage};}}

\begin{document}

\tableofcontents

\chapter{Intro} \startcontents[chapters] \printcontents[chapters]{p}{1}{} \lipsum[1-2]

\section{Sec1 I have title} \lipsum[1-9] \section{Sec2 Title goes here} \lipsum[3-8] \section{Sec3 Conditions goes here} \lipsum[3-7]

\chapter{Chap2} \startcontents[chapters] \printcontents[chapters]{p}{1}{}

\lipsum[3] \section{Sec1 This is a section} \lipsum[1-9] \section{Sec2 Final doc} \lipsum[3-8] \section{Sec3 Conditions goes here} \lipsum[3-7]

\end{document}

Output:

enter image description here

Problem is: I want the page numbers in the in-chapter contents appears in a line vertically equal. I have highlighted my need. The yellow marker shows the page number 3, 5 and 7 should appear in a straight line according to my choice about the distance. I can make it closer or far away.

Any suggestions? Thanks.

mmr
  • 2,249
  • 5
  • 22
  • I don't have a computer to try, but maybe try to add a \hfill before the drawing to push the drawing on the right, and adjust the distance with a negative space, something like \space{-2cm} – tobiasBora Dec 15 '21 at 07:01
  • @tobiasBora Where do I put the commands? in \titlecontents or elsewhere? – mmr Dec 15 '21 at 07:15

1 Answers1

3

By using \hfill and \hspace{...} you can do what you want (change the hspace value to put it closer or further from the right boundary):

enter image description here

\documentclass{memoir}
\usepackage{lipsum}
\usepackage{titletoc}
\usepackage{tikz}

\titlecontents{psection}[2.3em] {\large} {\contentslabel{2.5em}} {} {\hfill\tikz[baseline]{\node[draw, inner sep=3pt, rounded corners, fill=red!30, anchor=base]{\thecontentspage};}\hspace*{4cm}}

\begin{document}

\tableofcontents

\chapter{Intro} \startcontents[chapters] \printcontents[chapters]{p}{1}{} \lipsum[1-2]

\section{Sec1 I have title} \lipsum[1-9] \section{Sec2 Title goes here} \lipsum[3-8] \section{Sec3 Conditions goes here} \lipsum[3-7]

\chapter{Chap2} \startcontents[chapters] \printcontents[chapters]{p}{1}{}

\lipsum[3] \section{Sec1 This is a section} \lipsum[1-9] \section{Sec2 Final doc} \lipsum[3-8] \section{Sec3 Conditions goes here} \lipsum[3-7]

\end{document}

tobiasBora
  • 8,684