How to include only named sections or subsections in the table of contents
up vote
1
down vote
favorite
I have lots of little sections and subsections in an article. some of them have a title and some don't. Is it possible to have a table of contents where only the named sections/subsections appear in it?
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
I want only the sections and subsections named Alice, Ava, Bob, Charles
to show up in the table of contents.
table-of-contents
add a comment |
up vote
1
down vote
favorite
I have lots of little sections and subsections in an article. some of them have a title and some don't. Is it possible to have a table of contents where only the named sections/subsections appear in it?
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
I want only the sections and subsections named Alice, Ava, Bob, Charles
to show up in the table of contents.
table-of-contents
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have lots of little sections and subsections in an article. some of them have a title and some don't. Is it possible to have a table of contents where only the named sections/subsections appear in it?
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
I want only the sections and subsections named Alice, Ava, Bob, Charles
to show up in the table of contents.
table-of-contents
I have lots of little sections and subsections in an article. some of them have a title and some don't. Is it possible to have a table of contents where only the named sections/subsections appear in it?
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
I want only the sections and subsections named Alice, Ava, Bob, Charles
to show up in the table of contents.
table-of-contents
table-of-contents
edited Nov 20 at 7:44
asked Nov 20 at 2:40
smilingbuddha
7434915
7434915
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44
add a comment |
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
With a standard class you could patch @sect
:
documentclass{report}
usepackage{xpatch}
letoriginaladdcontentslineaddcontentsline
makeatletter
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
makeatother
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:
documentclass{scrreprt}
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
add a comment |
up vote
1
down vote
You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like
makeatletter
defl@section#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
fi}
defl@subsection#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
fi}
makeatother
that you need to put before begin{document}
.
This does not work, because the first argument ofl@section
orl@subscetion
incudesnumberline{<number>}
if the section or subsection is numbered.
– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
add a comment |
up vote
0
down vote
With etoc
you can have both types of tables of contents at the same time (with the standard classes).
documentclass{report}
usepackage{etoc}
newcommand{trimmedtableofcontents}{%
begingroup
etocsetstyle{chapter}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedchaptertocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedchaptertocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{section}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{subsection}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsubsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsubsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
tableofcontents
endgroup
}
begin{document}
trimmedtableofcontents
renewcommandcontentsname{Full table of contents}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
First page:
Second page:
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With a standard class you could patch @sect
:
documentclass{report}
usepackage{xpatch}
letoriginaladdcontentslineaddcontentsline
makeatletter
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
makeatother
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:
documentclass{scrreprt}
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
add a comment |
up vote
3
down vote
accepted
With a standard class you could patch @sect
:
documentclass{report}
usepackage{xpatch}
letoriginaladdcontentslineaddcontentsline
makeatletter
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
makeatother
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:
documentclass{scrreprt}
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With a standard class you could patch @sect
:
documentclass{report}
usepackage{xpatch}
letoriginaladdcontentslineaddcontentsline
makeatletter
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
makeatother
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:
documentclass{scrreprt}
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
With a standard class you could patch @sect
:
documentclass{report}
usepackage{xpatch}
letoriginaladdcontentslineaddcontentsline
makeatletter
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
xpatchcmd@sect
{addcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}}
{%
if!#7!else
originaladdcontentsline {toc}{#1}{ifnum #2>c@secnumdepth else protect numberline {csname the#1endcsname }fi #7}%
fi
}{}{PatchFailed}
makeatother
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
But you could also use a KOMA-Script class. Then there would be no TOC entry for empty section titles by default:
documentclass{scrreprt}
begin{document}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
Result:
edited Nov 20 at 14:54
answered Nov 20 at 11:08
esdd
57.8k34485
57.8k34485
add a comment |
add a comment |
up vote
1
down vote
You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like
makeatletter
defl@section#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
fi}
defl@subsection#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
fi}
makeatother
that you need to put before begin{document}
.
This does not work, because the first argument ofl@section
orl@subscetion
incudesnumberline{<number>}
if the section or subsection is numbered.
– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
add a comment |
up vote
1
down vote
You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like
makeatletter
defl@section#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
fi}
defl@subsection#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
fi}
makeatother
that you need to put before begin{document}
.
This does not work, because the first argument ofl@section
orl@subscetion
incudesnumberline{<number>}
if the section or subsection is numbered.
– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like
makeatletter
defl@section#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
fi}
defl@subsection#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
fi}
makeatother
that you need to put before begin{document}
.
You need to redefine the tex macros that render the toc entry. That said, what you need to edit depends on the document class and the packages you include into your document. For the simplest latex book, no packages used, that would be something like
makeatletter
defl@section#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}%
fi}
defl@subsection#1#2{%
globalletoldnumlinenumberline
defnumberline##1{}%
if!#1!
globalletnumberlineoldnumline
else
globalletnumberlineoldnumline
@dottedtocline{2}{3.8em}{3.2em}{#1}{#2}%
fi}
makeatother
that you need to put before begin{document}
.
edited Nov 20 at 12:58
answered Nov 20 at 8:02
Lupino
1,039610
1,039610
This does not work, because the first argument ofl@section
orl@subscetion
incudesnumberline{<number>}
if the section or subsection is numbered.
– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
add a comment |
This does not work, because the first argument ofl@section
orl@subscetion
incudesnumberline{<number>}
if the section or subsection is numbered.
– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
This does not work, because the first argument of
l@section
or l@subscetion
incudes numberline{<number>}
if the section or subsection is numbered.– esdd
Nov 20 at 12:31
This does not work, because the first argument of
l@section
or l@subscetion
incudes numberline{<number>}
if the section or subsection is numbered.– esdd
Nov 20 at 12:31
@esdd good point…
– Lupino
Nov 20 at 12:48
@esdd good point…
– Lupino
Nov 20 at 12:48
add a comment |
up vote
0
down vote
With etoc
you can have both types of tables of contents at the same time (with the standard classes).
documentclass{report}
usepackage{etoc}
newcommand{trimmedtableofcontents}{%
begingroup
etocsetstyle{chapter}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedchaptertocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedchaptertocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{section}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{subsection}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsubsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsubsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
tableofcontents
endgroup
}
begin{document}
trimmedtableofcontents
renewcommandcontentsname{Full table of contents}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
First page:
Second page:
add a comment |
up vote
0
down vote
With etoc
you can have both types of tables of contents at the same time (with the standard classes).
documentclass{report}
usepackage{etoc}
newcommand{trimmedtableofcontents}{%
begingroup
etocsetstyle{chapter}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedchaptertocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedchaptertocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{section}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{subsection}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsubsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsubsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
tableofcontents
endgroup
}
begin{document}
trimmedtableofcontents
renewcommandcontentsname{Full table of contents}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
First page:
Second page:
add a comment |
up vote
0
down vote
up vote
0
down vote
With etoc
you can have both types of tables of contents at the same time (with the standard classes).
documentclass{report}
usepackage{etoc}
newcommand{trimmedtableofcontents}{%
begingroup
etocsetstyle{chapter}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedchaptertocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedchaptertocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{section}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{subsection}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsubsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsubsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
tableofcontents
endgroup
}
begin{document}
trimmedtableofcontents
renewcommandcontentsname{Full table of contents}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
First page:
Second page:
With etoc
you can have both types of tables of contents at the same time (with the standard classes).
documentclass{report}
usepackage{etoc}
newcommand{trimmedtableofcontents}{%
begingroup
etocsetstyle{chapter}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedchaptertocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedchaptertocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{section}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
etocsetstyle{subsection}
{}
{ifxetocthenameempty
else
etocifnumbered
{etocsavedsubsectiontocline
{numberline{etocnumber}etocname}{etocpage}}
{etocsavedsubsectiontocline{etocname}{etocpage}}%
fi
}%
{}
{}
tableofcontents
endgroup
}
begin{document}
trimmedtableofcontents
renewcommandcontentsname{Full table of contents}
tableofcontents
chapter{My chapter}
section{Alice}
section{}
subsection{}
subsection{Ava}
section{Bob}
section{}
section{}
section{Charles}
end{document}
First page:
Second page:
answered Nov 21 at 8:58
jfbu
44.5k65143
44.5k65143
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f460868%2fhow-to-include-only-named-sections-or-subsections-in-the-table-of-contents%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When you say "index" are you meaning "table of contents" ? Please be more specific, and help us to help you by providing a minimal working example (edit your post to add code).
– Jhor
Nov 20 at 7:21
Yes thats what I meant...fixed the title. thanks
– smilingbuddha
Nov 20 at 7:44