2

I'm using \setcounter{secnumdepth}{0} to show sections without any number. It works as expected, but I'd like to have some indentation to align the begin of the text.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{blindtext}

\usepackage{filecontents}
\begin{filecontents}{text1.tex}
     \section{text 1}
    \blindtext
\end{filecontents}
\begin{filecontents}{text2.tex}
    \section{text 2}
    \blindtext
\end{filecontents}
\begin{filecontents}{text3.tex}
    \section{text 3}
    \blindtext
\end{filecontents}
\begin{filecontents}{text4.tex}
    \section{text 4}
\blindtext
\end{filecontents}

\newcommand{\inputa}[1]{%
    \setcounter{secnumdepth}{0} 
    \input{#1}
    \setcounter{secnumdepth}{1}
}

\begin{document}    
\input{text1.tex}
\inputa{text2.tex}
\input{text3.tex}
\input{text4.tex}

\tableofcontents

\end{document}

Giving the following result: unaligned entries

I'd like all the words "text" to be aligned.

[EDIT after Phelype Oleinik answere]

Regarding the suggested non-starred approach: I will not manually change my numerous sections consisting of a lot of different .tex files. I've once tried to make it automated writing my \inputa command with something like:

\newcounter{starlab}%counter for a label to use with \nameref
\newcommand{\inputa}[1]{%
    \let\origsection\section
    \renewcommand{\section}{% force starred section
        \@ifstar{\origsection*}{\origsection*}}
    \input{#1}
    \refstepcounter{starlab}\label{sec:\thestarlab}
    \addcontentsline{toc}{section}{\protect\numberline{}\nameref{sec:\thestarlab}}
    \let\orgisection\section
    }

But…

  1. I cannot compile it anymore, I don't know why (Argument of \@ssect has an extra } error).
  2. yersterday it worked on my document with the desired appearance, but I still had a problem with hypperef since the label points at the end of the text, and those texts are sometimes quite long.
2

You can do it with etoc.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{etoc}
\etocsetstyle{section}
    {}
    {\etocifnumbered
      {\etocsavedsectiontocline{\numberline{\etocnumber}\etocname}{\etocpage}}
      {\etocsavedsectiontocline{\numberline{}\etocname}{\etocpage}}%
    }
    {}
    {}
\begin{document}
\tableofcontents
\section{Alpaca}
\blindtext
\setcounter{secnumdepth}{0}
\section{Capybara}
\setcounter{secnumdepth}{1}
\blindtext
\section{Ant}
\blindtext
\end{document}

enter image description here

Here is with MWE from OP.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{blindtext}
\usepackage{etoc}
\etocsetstyle{section}
    {}
    {\etocifnumbered
      {\etocsavedsectiontocline{\numberline{\etocnumber}\etocname}{\etocpage}}
      {\etocsavedsectiontocline{\numberline{}\etocname}{\etocpage}}%
    }
    {}
    {}

\usepackage{filecontents}
\begin{filecontents}{text1.tex}
     \section{text 1}
    \blindtext
\end{filecontents}
\begin{filecontents}{text2.tex}
    \section{text 2}
    \blindtext
\end{filecontents}
\begin{filecontents}{text3.tex}
    \section{text 3}
    \blindtext
\end{filecontents}
\begin{filecontents}{text4.tex}
    \section{text 4}
\blindtext
\end{filecontents}

\newcommand{\inputa}[1]{%
    \setcounter{secnumdepth}{0} 
    \input{#1}
    \setcounter{secnumdepth}{1}
}

\begin{document}    
\input{text1.tex}
\inputa{text2.tex}
\input{text3.tex}
\input{text4.tex}

\tableofcontents

\end{document}

enter image description here

| improve this answer | |
  • this solution supposes the table of contents contains no subsection etc... but it can easily be extended to cope with it. (see 18. Another compatibility mode in etoc documentation. – user4686 May 31 '18 at 10:47
  • Nice package, I'm using it and I will read the documentation. – omisson May 31 '18 at 14:27
5

I changed your approach a bit... Instead of changing secnumdepth in the middle of the document, you can use the starred version \section* to add a numberless section.

(If you insist on changing secnumdepth, see the second part of the answer).

Then you can use \addcontentsline{toc}{section}{text 2} to add this section's entry to the ToC.

Better yet! You can use

\section*{text 2}
\addcontentsline{toc}{section}{\numberline{}text 2}

and the indentation will be preserved in the ToC, as you want:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{blindtext}

\begin{document}
\section{text 1}
\blindtext

\section*{text 2}
\addcontentsline{toc}{section}{\numberline{}text 2}
\blindtext

\section{text 3}
\blindtext
\section{text 4}
\blindtext

\tableofcontents

\end{document}

To make things less manual, you can define a command, say, \nonumbersection to do the whole thing:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{blindtext}


\makeatletter
\def\nonumbersection{\@dblarg\@nnsection}
\def\@nnsection[#1]#2{%
  \section*{#2}%
  \sectionmark{#1}%
  \addcontentsline{toc}{section}{\numberline{}#1}}
\makeatother

\begin{document}
\section{text 1}
\blindtext

\nonumbersection{text 2}
\blindtext

\section{text 3}
\blindtext
\section{text 4}
\blindtext

\nonumbersection[t5]{text 5}
\blindtext

\tableofcontents

\end{document}

As per your edit, if you insist on keeping the secnumdepth approach:

Your definition of \inputa is almost correct, except:

  • The \refstepcounter, \label, and \addcontentsline commands should me inside the new definition of \section, because this \section command will be inside the \input, and if you place the \label after the \input it's too late, and the reference will be wrong (as you concluded yourself);

  • The de-redefinition of \section is incorrect. You had \let\orgisection\section, which will make a copy of \section into \orgisection, but you want the opposite, so it's \let\section\orgisection;

  • It's \origsection :P

After the corrections, your code works as expected:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{blindtext}

\usepackage{filecontents}
\begin{filecontents}{text1.tex}
\section{text 1}
\blindtext
\end{filecontents}
\begin{filecontents}{text2.tex}
\section{text 2}
\blindtext
\end{filecontents}
\begin{filecontents}{text3.tex}
\section{text 3}
\blindtext
\end{filecontents}
\begin{filecontents}{text4.tex}
\section{text 4}
\blindtext
\end{filecontents}

\makeatletter
\newcounter{starlab}%counter for a label to use with \nameref
\newcommand{\inputa}[1]{%
    \let\origsection\section
    \def\section{%
      \refstepcounter{starlab}%
      \label{sec:\thestarlab}%
      \addcontentsline{toc}{section}{\protect\numberline{}\nameref{sec:\thestarlab}}
      \@ifstar{\origsection*}{\origsection*}}
    \input{#1}
    \let\section\origsection}
\makeatother

\begin{document}
\input{text1.tex}
\inputa{text2.tex}
\input{text3.tex}
\input{text4.tex}

\tableofcontents

\end{document}
| improve this answer | |
  • In a document with running heads one might also want \sectionmark. And maybe \nonumbersection could support the optional short argument. – moewe May 30 '18 at 16:54
  • Thanks for your answer, but I would prefer not to change my numerous sections which are on different texfiles. I've edited my answer to be more precise. – omisson May 30 '18 at 18:10
  • @moewe I updated my answer to incorporate your suggestions. Thanks :) – Phelype Oleinik May 30 '18 at 22:01
  • @omisson I updated my answer with a version that works (I hope) with the structure of your document. – Phelype Oleinik May 30 '18 at 22:02
  • Unfortunately, there is a conflict somewhere with my custom documentclass and y get double numbered entries with this code. But it was smart and very interesting to read. Finally, I'm using the etoc package. – omisson May 31 '18 at 14:26
4

Using unnumberedtotoc:

omissonUnnumbered

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[indentunnumbered]{unnumberedtotoc}%<----------
\usepackage{hyperref}
\begin{document}
\tableofcontents
\section{Alpaca}
\blindtext
\addsec[Wombat]{Capybara}
\blindtext
\section{Ant}
\blindtext
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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