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!

  • * does not really belong to the macro name \section, it's an argument for \section here, and \let\origsection\section* leaves * in the input stream, causing the error – user31729 Oct 4 '17 at 18:27
  • 4
    Why don't you simply change the counter secnumdepth? – Ulrike Fischer Oct 4 '17 at 18:30
  • 1
    @UlrikeFischer can you post that as an answer:-) – David Carlisle Oct 4 '17 at 18:40
  • @DavidCarlisle Done. – Ulrike Fischer Oct 4 '17 at 18:46
  • 1
    This is an XY-problem: Different question, with a similar solution as Remove numbering from Section and Subsection (possible duplicate). – Werner Oct 4 '17 at 19:40
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 4 '17 at 18:51
  • This "works", but setting secnumdepth (as in the other answer) is a better way to solve your problem. – alephzero Oct 4 '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 – user31729 Oct 4 '17 at 19:12
  • @aidangallagher4: Please unaccept the answer. I rather delete my answer than keeping it treated this way – user31729 Oct 4 '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 4 '17 at 19:51
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

  • Upvote for the shortest solution! – aidangallagher4 Oct 4 '17 at 18:50
  • 6
    @aidangallagher4 it's also the solution you should accept. (Christian won't mind giving up his tick to a lady:-) – David Carlisle Oct 4 '17 at 18:54
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 
  • Isn't this too complicated? – user31729 Oct 4 '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 4 '17 at 19:45

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.