# Customize \section

I have a document, where I want typical code: "Problem # n" to be printed in every section. I made this code, which is working. How can I get rid of \setcounter{secnumdepth}{1} and \addtocounter{secnumdepth}{1}?

\documentclass{article}
\setcounter{secnumdepth}{1}
\renewcommand{\section}[1]{\begin{center}\Large{\bf{Problem \# \arabic{secnumdepth}}}\end{center}
\begin{document}
\section{}
text
\section{}
text
\section{}
text
\end{document}


-
Surely secnumdepth is not the counter to use here. –  egreg Nov 2 '12 at 13:44
you might consider treating this as a theorem-class environment where you can relatively freely define the heading text and the counter is supplied automatically. you'd want to use a theorem package that provides several different styles (unlike the article default), since you most likely don't want the text in italic. –  barbara beeton Nov 2 '12 at 13:56
Please note that neither \Large nor \bf are commands that take an argument. Thus, it's not necessary to encase the subsequent material in braces. The scope of these two commands will end automatically when the \end{center} instruction is encountered. –  Mico Nov 2 '12 at 15:20

The counter secnumdepth controls the level up to which sectional units will be numbered, so it doen't make much sense to use this counter to number your structure.

One possibility using the titlesec package:

\documentclass{article}
\usepackage{titlesec}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{Problem \# \thesection}
{0em}{}

\begin{document}
\section{}
text
\section{}
text
\section{}
text
\end{document}


Perhaps you could consider defining a dedicated command/environmet for this instead of (ab)using \section?

Here's a better option, using a theorem-like structure defined with the help of the amsthm package; in this way, the counter is provided and increased automatically (feel free to make the adjustments that best suit your needs):

\documentclass{article}
\usepackage{amsthm}

\newtheoremstyle{mystyle}
{\topsep}{\topsep}{\normalfont}{}{\Large\bfseries}{\newline}{0em}
{\hfil\thmname{#1 \#}\thmnumber{ #2}\thmnote{#3}\hfil}
\theoremstyle{mystyle}
\newtheorem{prob}{Problem}

\begin{document}

\begin{prob}
text
\end{prob}

\begin{prob}
text
\end{prob}

\end{document}


As a side note, \bf is an obsolete command (only provided for compatibility); you should use \bfseries instead; additionally \bf and \bfseries are declarations that do not take arguments; they are to be used as in {\bfseries text} (the braces are only to keep the effect local to a group).

-
Thx, but I believe, your code doesn't give any benefit to mine in regard of number of lines. In my answer I got rid of initial setting the counter to 1. But couldn't get rid of increase operator. –  user4035 Nov 2 '12 at 14:11
@user4035 I added another option to my answer, using a dedicated theorem-like structure. –  Gonzalo Medina Nov 2 '12 at 14:35
@user4035 I don't care much about number of lines of the code, as long as it is syntactically and semantically correct. –  Gonzalo Medina Nov 2 '12 at 17:44

I got rid of \setcounter{secnumdepth}{1}, but still have to increase the counter every time:

\documentclass{article}