3

I have this simple Idea:

I want to have LaTeX keep count of the number of text-parts (which already works fine):

\newcommand{\newpart}{
    \bigskip
    \begin{center}
            \arabic{partscounter}.
    \end{center}
    \stepcounter{partscounter}
}

I want to do this with it:

\newpart
Long Text Number 1
\newpart
Long Text Number 2
...

Between those 2 texts it should say the number of the 2 texts, e.g.:

1.
Long Text Number 1
2.
Long Text Number 2

As said, this works. But now I came across a problem: Sometimes, the text is so long that it will go to the next page. And sometimes, only one line was on the previous page, in these cases, I have:

2. 
------------- % New Page!
Long Text 2

where the partcounter is the last line of a page.

I don't want this behaviour to occur. If the number printed by \newpart is the last line, I want to skip that line and have it like this:

1.
Long Text 2
------------- % New Page!

2. 
Long Text 2

Is that possible?

4

David's idea is good, but unfortunately it doesn't work, because there's always a feasible page break point after \end{center}, no matter a \nopagebreak command is issued before it.

This seems to work better.

\documentclass{article}
%% geometry is just to have a reduced text height
%% for this example
\usepackage[showframe,textheight=3.514cm,heightrounded]{geometry}
\newcounter{partscounter}

\newcommand{\newpart}{%
  \par\addvspace{\bigskipamount}
  {\centering
   \leavevmode\stepcounter{partscounter}%
   \thepartscounter.\par\nopagebreak
   \addvspace{\topsep}}
}

\begin{document}

\newpart
Text follows
\newpart
Text follows
\newpart
Text follows

\end{document}

The third part goes entirely to the new page, but if 3.514cm is changed into 3.515cm, the third part fits in the page.

  • +1 for addvspace (but using a real sectioning command would of course do that anyway:-) – David Carlisle Oct 6 '13 at 22:32
3

The standard LaTeX heading code has a lot of code to take care of this, why not simply use \part{} instead of making a heading by hand?

If you do want to do it by hand the normal latex style id to increment before the heading, not after, so that the counter has the correct value during that part, and also use \refstepcounter so you can \label it. \nopagebreak should discourage page breaking.

\newcommand{\newpart}{%
    \bigskip
    {\centering
            \leavevmode\refstepcounter{partscounter}%
             \arabic{partscounter}.\par\nopagebreak
    }}
}

But it is better to use \part and customise the heading code if needed.

  • With center it doesn't work; probably it's does with \centering. – egreg Oct 6 '13 at 21:13

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.