1

I have a confusion in the toc between the title and the dots.

 \documentclass[oneside]{book}

    \usepackage{color, xcolor}


    \newcommand{\mysection}[2]{
        \setcounter{section}{#1}
        \setcounter{section}{0}
        \chapter*{#2}
        \addcontentsline{toc}{section}
    {\protect\numberline{\hspace{-1.15cm}%
                       \colorbox{blue!50}{\color{blue}\string\parbox[c][.72em]{1em}
                       {\strut\protect}}%
                       \hspace{1em}\bfseries\sffamily \MakeUppercase{#2}\protect}}
    }




    \begin{document}

    \tableofcontents

    \frontmatter
    \mysection{1}{Introduction}
    \mysection{0}{Acknowledgments}
    \mainmatter


    \end{document}

enter image description here

2
  • 1
    Your \colorbox leaks into the title region, as far as I can see. There are better ways to achieve such things with tocloft, for example
    – user31729
    Jun 29, 2016 at 18:13
  • What are you trying to achieve here? You're using a \section-like interface yet you're setting a \chapter... Could you elaborate?
    – Werner
    Jun 29, 2016 at 18:14

2 Answers 2

1

Instead of a bunch of \protect commands and a strange \string macro there, it's better to use a robust command for such things since exandability of the command is no issue here!

However, \chapter* and using section as formatter indicator for the ToC still looks weird.

\documentclass[oneside]{book}

\usepackage{xcolor}

\usepackage{tocloft}

\renewcommand{\cftsecindent}{0pt}  % Perhaps?

\DeclareRobustCommand{\entryfortoc}[1]{%
  \bfseries\sffamily\MakeUppercase{#1}%
}

\DeclareRobustCommand{\bluenumberbox}[1]{%
  \colorbox{blue!50}{\color{blue}\parbox[c][.72em]{1em}{\strut}}%
}

\newcommand{\mysection}[2]{%
  \setcounter{section}{#1}
  \setcounter{section}{0}
  \chapter*{#2}
  \addcontentsline{toc}{section}{\protect\numberline{\bluenumberbox{}}\entryfortoc{#2}}%
}



\begin{document}

\tableofcontents

\frontmatter
\mysection{1}{Introduction}
\mysection{0}{Acknowledgments}
\mainmatter


\end{document}

enter image description here

0

It's quite hard to understand what you're trying to do.

  1. Why should chapters in the front matter be regarded at the section level in the TOC?

  2. Should the blue rectangle be typeset in the margin?

  3. Why not simply a rule?

  4. What's the purpose of \setcounter{section}{#1}\setcounter{section}{0} if the final effect is just to set the section counter to 0?

Here's a simpler code.

\documentclass[oneside]{book}

\usepackage{xcolor}

\DeclareRobustCommand{\bluerectangle}{%
  \hspace*{-3.5em}%
  \textcolor{blue!50}{\rule{1em}{.72em}}%
  \quad
}
\newcommand{\mysection}[1]{%
  \stepcounter{section}% reset all child counters
  \setcounter{section}{0}%
  \chapter*{#1}
  \addcontentsline{toc}{section}{%
    \bluerectangle\bfseries\sffamily\MakeUppercase{#1}%
  }%
}

\begin{document}

\tableofcontents

\frontmatter
\mysection{Introduction}
\mysection{Acknowledgments}

\end{document}

enter image description here

You must log in to answer this question.

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