4

Code:

\documentclass{article}

\newcounter{mycounter}
\setcounter{mycounter}{1}

\begin{document}
    \paragraph{\arabic{mycounter}}%
    \stepcounter{mycounter}%
    \paragraph{\arabic{mycounter}}%
\end{document}

Expected:

1
2

Actual:

2
2

I am very confused, can someone explain what is happening?

5

\paragraph (like most headings) does not actually start a paragraph by setting anything. This is done to avoid headings ending up at the bottom of the page and being disconnected from what follows it. You'll have to set some regular text (even if that text is an empty box \mbox{}, say) in order for the paragraph headings to actually be set, and therefore display the correct numbering sequence.

enter image description here

\documentclass{article}

\newcounter{mycounter}
\setcounter{mycounter}{1}

\begin{document}

\paragraph{\themycounter}
Something% You need something here to set the above \paragraph heading

\stepcounter{mycounter}%
\paragraph{\themycounter}

\end{document}
  • However, if I use subsubsections instead of paragraphs, the code works as intended (as presented it prints 0.0.1 1 then 0.0.2 2). What is different? Even if i remove all other text with \renewcommand{\thesubsubsection}{} (In this case it prints 1 then 2) – denmesh Jun 24 at 11:00
  • 1
    @denmesh the default setting of \paragraph is as an inline heading unlike that of \subsubsection which is display, inline headings are held back and inserted at the start of the next paragraph, once that paragraph starts. – David Carlisle Jun 24 at 11:04
  • 1
    @denmesh: You can change sectional headings to be inline or display, and they would behave differently depending on that setting. Different classes define headings to suit their needs and therefore may react that what is expected. – Werner Jun 24 at 15:08
3

enter image description here

\paragraph, like all sectioning commands, already has a dedicated counter that it increments so all you need is:

\documentclass{article}

\setcounter{secnumdepth}{4}
\renewcommand\theparagraph{\arabic{paragraph}}

\begin{document}
    \paragraph{}

    \paragraph{}
\end{document}

However as \paragraph is a 4th level heading it should not be used, except after the 3rd level heading, \subsubsection

  • This I do know, and be assured, if I didn't strictly need to use a separate counter, I wouldn't. – denmesh Jun 24 at 10:52
  • @denmesh if you need a new counter then it is usually easier/more robust to hook it to the counter operations so you can make your new counter reset whenever paragraph is stepped, or be an alias for paragraph or whatever. – David Carlisle Jun 24 at 11:02

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.