4

I'm sorry if this is a duplicate, however I couldn't seem to find an exact answer to this anywhere. I am typing up a paper in LaTeX, and for ease of typing (although not that much admittedly), I would like to redefine the \section command so it doesn't give section numbers (ie is the same as \section*).

Initially, I naively tried \renewcommand{\section}{\section*}, however of course this led to an infinite recursion (since \section* is defined using \section), so didn't work. Next, having done a bit of research, it appeared I could circumvent this by doing (in the preamble):

\let\oldsection\section*
\renewcommand{\section}{\oldsection}

However this returned the error:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.13 \let\oldsection\section*

I believe the error relates to the use of \section* in the \let command, however am not sure exactly what is wrong.

I know I could sort this out by doing:

\let\oldsection\section
\renewcommand{\section*}{\oldsection}

however I feel this isn't in the spirit of laziness which started me in this, and I would like (if possible), a solution which still allows me to use \section it optimal.

I suspect the solution may lie in a redefinition of the numbering rules for \section (which would be ok, however not ideal), however any help is greatly appreciated!

3 Answers3

17

I would set secnumdepth if I want unnumbered sections:

\documentclass{article}

\setcounter{secnumdepth}{0}

\begin{document}

\section{Foo}


\end{document}

enter image description here

Ulrike Fischer
  • 327,261
6

* is not part of the macro name, it's an argument effectively -- macros that have a starred version use \@ifstar to check whether the next token is a * and branch into a different macro that performs the expected operation for the starred version and another one for the unstarred macro.

\let\origsection\section stores the old definition, the version here acts
always for the \oldsection* version.

\documentclass{article}

\let\origsection\section

\makeatletter

\renewcommand{\section}{%
  \@ifstar{%
    \origsection*%
  }{%
    \origsection*%
  }%
}

\makeatother

\begin{document}

\section{Foo}

\section*{Foo again}

\end{document}
  • Thanks very much this is what I was looking for; would it be possible to explain how this code works for extra kudos? – aidangallagher4 Oct 04 '17 at 18:51
  • This "works", but setting secnumdepth (as in the other answer) is a better way to solve your problem. – alephzero Oct 04 '17 at 18:53
  • @alephzero: better is a opinion-based view ;-) My version is useful for cases where secnumdepth is not meant, i.e. when other commands other than the structure macros should behave like their starred version –  Oct 04 '17 at 19:12
  • @aidangallagher4: Please unaccept the answer. I rather delete my answer than keeping it treated this way –  Oct 04 '17 at 19:34
  • At the risk of engaging in this clearly rather partisan issue, I’m going to stick to my guns and keep your answer as my chosen one, as, for the reasons you and I have already laid out, I believe it best fits my question. Questions over aesthetics are highly subjective, and no doubt everybody has their opinion, however given that this is my question (though my number of points pale compared with anyone else here), I am going to make the executive decision here – aidangallagher4 Oct 04 '17 at 19:51
  • @alephzero I don't know which one is "better" in the sense that it fits the goal of the OP. In my experience in 99% of the cases people asking such a question don't know about secnumdepth. But there is still the 1% which has some special problem -- that's why I asked at first in the comment about the reason secnumdepth is not used. – Ulrike Fischer Oct 04 '17 at 19:56
  • @UlrikeFischer: I had the secnumdepth way in my mind too, but as soon as you posted your comment, I had to find another way. –  Oct 04 '17 at 19:57
  • @aidangallagher4: Again, please unaccept my answer. Its rating is ridiculous compared to other ones. I don't want to keep it –  Oct 04 '17 at 20:23
  • Rating too high or too low? I feel it's a bit strange to answer the question and then complain when the asker says "thanks, that's what I wanted to know". – Mark Oct 05 '17 at 03:11
4

Try this:

https://tex.stackexchange.com/a/380116/120578

Especially for your needs it can be like:

\let\oldsection\section
\makeatletter
\def\msection{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have optional parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{#2}%
}
\def\@StarredWithout#1{
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection*{#2}%
}
\def\@nonStarredWithout#1{%
\oldsection*{#1}%
}
\makeatother 
koleygr
  • 20,105
  • Isn't this too complicated? –  Oct 04 '17 at 19:34
  • @ChristianHupfer I just changed the code of the link. I could just leave out the nonStarred and call the starred... But I left it like this in case someone wants to do something more in any case (different for starred or nonStarred etc). It has the advantage that accepts Starred section with optional argument (that OP seems to want) -my code just ignores it but could use it for fancyhead or whatever- . So I think that the code of the link is not too complicated, but almost the minimum needed that gives the opportunity to take care of every possibility and do whatever y want with sections. – koleygr Oct 04 '17 at 19:45