Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\addtocounter{secnumdepth}{1}}
\begin{document}
\section{}
text
\section{}
text
\section{}
text
\end{document}

enter image description here

share|improve this question
2  
Surely secnumdepth is not the counter to use here. –  egreg Nov 2 '12 at 13:44
1  
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
1  
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

2 Answers 2

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).

share|improve this answer
    
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
up vote 0 down vote accepted

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

\documentclass{article}
\renewcommand{\section}[1]{\addtocounter{section}{1}\begin{center}\Large{\bf{Problem \# \arabic{section}}}\end{center}}
\begin{document}
\section{}
text
\section{}
text
\section{}
text
\end{document}
share|improve this answer
    
A potentially serious issue with your proposed solution (as well as the OP's originally-proposed method...) is that a "section header" line could end up stranded as the last item on a page -- an offense against good typography. LaTeX's ordinary sectioning commands provide a lot of code to avoid just such outcomes. –  Mico Nov 2 '12 at 15:32
    
Yes, I must manually avoid it. –  user4035 Nov 2 '12 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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