3

I am writing a lab report for a class. I want to redefine \section so that \section{Analysis} has the same output that \section*{\centering Analysis} would normally have. I tried

\renewcommand{\section}[1]{\section*{\centering #1}}

but this causes an error. I am able to do

\newcommand{\newsection}[1]{\section*{\centering #1}}

to define a new command to do this, but it would be ideal if I could use the same \section command. I've seen several other questions about similar topics, but I don't understand the code so I can't quite adapt it to my example. My code would be something like this:

\documentclass{article}

% redefine the command

\begin{document}

blah blah blah

\section{Analysis}

blah blah blah

\end{document}

Any help (regarding commands or any other aspects of my code that you find inappropriate) is appreciated.

  • 3
    This is a bad approach, have a look at the titlesec package instead. – daleif Oct 27 '18 at 18:23
  • 1
    Your idea doesn't work because \section* is not actually a command. The * in your input is treated as an optional first argument to the \section command. You can demonstrate that by seeing what happens if you input \section * {my section title} with spaces either side of the *. – alephzero Oct 27 '18 at 18:52

1 Answers1

4

Two things here:

  1. Formatting

    If you want a consistent format for sectioning components, use a package that ties into that part of the document. I've used sectsty below. See Centering chapter/section/subsection.

  2. Redefinition

    Your redefinition is circular, since \section uses \section. Instead, copy \section into something else, say, \oldsection and then redefine \section using \oldsection. Something like

    \let\oldsection\section % Copy \section into \oldsection
    \renewcommand{\section}{\oldsection*} % \section is always starred
    

enter image description here

\documentclass{article}

\usepackage{lipsum}% Just for this example
\usepackage{sectsty}
\sectionfont{\centering}
\let\oldsection\section % Copy \section into \oldsection
\renewcommand{\section}{\oldsection*} % \section is always starred

\begin{document}

\lipsum[1]

\section{Analysis}

\lipsum[2]

\end{document}
Werner
  • 603,163