2

I have a problem with table of contents. I have changed the numbers of sections, that instead of numbers I have words, lets say "Dodatok A". But now, when the "number" of section is longer, the title of section and the "Dodatok A" are crossing, so it is not readable. Is there any way how to adjust the width of box for the number of section? I put here a picture with a part of table of contents, where you can see the problem. Thank you.

    \documentclass[12pt,a4paper]{article} 
    \usepackage[slovak]{babel} 
    \usepackage[utf8x]{inputenc} 
    \usepackage[T1]{fontenc} 
    \usepackage{palatino,verbatim}  
    \usepackage{graphicx} 
    \usepackage{multirow}  
    \usepackage{amsmath} 
    \usepackage{pstricks}
    \usepackage{wrapfig} 
    \usepackage{epsfig} 
    \usepackage{caption}
    \usepackage{subcaption}
   \usepackage{color} 
   \linespread{1.3} 
   \usepackage[total={17cm,25cm}, top=2.5cm, bottom=2.5cm, left=3.5cm, right=2cm, includefoot]{geometry}  
\usepackage{xr}
\usepackage{float}
\usepackage{hyperref} 
\usepackage{pdfpages}
\usepackage{calc,etoolbox}
\usepackage[titletoc,toc,title]{appendix}
\numberwithin{equation}{section}

     \begin{document} 
     \tableofcontents 

    some sections, subsections, figures etc.

    \setcounter{section}{0}
      \setcounter{subsection}{0}
      \renewcommand\thesection{Dodatok \Alph{section}}

     \setcounter{figure}{0}
      \setcounter{equation}{0}
      \renewcommand\thefigure{\Alph{section}\arabic{figure}}
      \renewcommand\theequation{\Alph{section}\arabic{equation}}

    \section{Kalibračná symetria pre elektromagnetizmus}
    text text 

     \end{document}

A picture of a part of my table of contents, where you can see my problem.

marked as duplicate by Marco Daniel, barbara beeton, Claudio Fiandrino, Gonzalo Medina, Xavier May 9 '13 at 17:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @MarcoDaniel: I would agree. Your answer should be adjusted though to incorporate some difference when using a class like article compared to book and/or report. article.cls defines \l@section differently to that of book.cls and report.cls and therefore requires different treatment. – Werner May 9 '13 at 16:30
  • Suzie, you can drop the use of epsfig as it is deprecated. – Werner May 9 '13 at 16:31
  • @Werner: But only if you don't use any related package. – Marco Daniel May 9 '13 at 16:31
2

This sort of adjustment is usually left for packages like tocloft. However, a manual adjustment is also possible by inserting content for the adjustment into the ToC at the right time. The use of babel does make things a tad trickier, since babel defines certain language-specific shorthands. These shorthands influence the capability to patch (or modify) commands on the fly. In particular, etoolbox is used to patch \l@section - the macro responsible for setting section-style entries in the ToC - while temporarily turning off the shorthand for -; set active by babel:

enter image description here

\documentclass{article}
\usepackage{calc,etoolbox}% http://ctan.org/pkg/{calc,etoolbox}
\usepackage[slovak]{babel}% http://ctan.org/pkg/babel
\begin{document}

\tableofcontents

\section{A section}

\renewcommand{\thesection}{Section~\Alph{section}}
\makeatletter
\addtocontents{toc}{%
  \protect\shorthandoff{-}% Remove 'active'ness of '-'
  \protect\patchcmd{\protect\l@section}{1.5em}{\widthof{\textbf{Section X\quad}}}{}{}%
  \protect\shorthandon{-}}% Make '-' active again
\makeatother

\section{Another section}

\end{document}

Since \tableofcontents reads content from the .toc file, it's appearance is usually delayed by at least one compilation. To that end, and somewhat counter-intuitive for those unfamiliar with TeX's auxiliary file usage, you need to insert the ToC-related adjustment within your document. The above does this, by inserting a patch to replace to default 1.5em space allowed for the section number with \widthof{\textbf{Section X\quad}} - the new width of section "numbers."

  • Thank you very much. I must be doing something wrong, but it doesn't work. I made this changes in my code: 1. add \usepackage{calc,etoolbox} to the preamble, 2. add \makeatletter \addtocontents{toc}{\protect\patchcmd{\protect\l@section}{1.5em}{\widthof{\textbf{Section X\quad}}}{}{}} \makeatother above the \section{Kalibračná symetria pre elektromagnetizmus} . Is it good? – Suzie May 9 '13 at 17:02
  • @Suzie: Yes, that looks good (just like I did in my MWE). You mention that "it doesn't work". What exactly? Does a compilation produce an error? Is there no change? When you look at <filename>.toc in your working folder, is there anything in there that resembles \patchcmd like inserted above? Are you using any ToC-related packages (I tested it even with hyperref and there's no problem). – Werner May 9 '13 at 17:46
  • @Werner No error, but no changes. I am using only those packages, that are included in my code (I have edited it so now there are all). In the .toc file, there is this: \patchcmd {\l@section }{1.5em}{\widthof {\textbf {Section X\hskip 1em\relax }}}{}{}. – Suzie May 9 '13 at 18:39
  • @Suzie: This must mean that the patch is not working. Change the last two empty arguments {}{} in the \patchcmd to {\protect\typeout{success}}{\protect\typeout{failure}}. This should output success in your .log if the patch was successful and failure otherwise. I see success when I review the .log file. What do you see? – Werner May 9 '13 at 18:53
  • ...moreover, what about using some of the suggestions within How to modify the indentation before sectioning titles in the table of contents? to correct the behaviour? Mine does a direct/manual adjustment. However, you could also use other packages, if this doesn't work for you. – Werner May 9 '13 at 19:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.