2

I am using the book class to write a document that will have numbered paragraphs. Wanted the (auto-incremented) paragraph numbers to be bold and centred, then the text below, and the paragraph number to have a label so it can be referenced. Using other people's suggestions, I've got this function/command:

\newcounter{para}
\newcommand\mypara[1]{
    \par\refstepcounter{para}\vfill
    \begin{center}\textbf{\thepara}\end{center}\label{#1}
    }

and the way I use it in the document is:

\mypara{myreference}
This is a nice numbered paragraph. I don't want any braces around the paragraph body, if at all possible.

which is working nicely EXCEPT when I use it close to the bottom of the page, in which case it's possible for the bold paragraph number to be on one page, and the text of the paragraph to be on the next. Is it possible to prevent a page break like this? I'm using multicols{2} as well, if that's relevant. Any help appreciated.

Min. example:

\documentclass[10pt]{book}
\usepackage{multicol}
\setlength\columnsep{1cm}  % sets the distance between columns
\usepackage{blindtext}

\newcounter{para}
\newcommand\mypara[1]{
    \par\refstepcounter{para}\vfill
    \begin{center}\textbf{\thepara}\end{center}\label{#1}
    }

\title{Minimum working example}

\begin{document}
    \pagestyle{empty}
    % main part of the document:

    \mainmatter
    \begin{multicols}{2}

        \mypara{ref1}
        \blindtext

        \mypara{ref2}
        \blindtext

        \mypara{ref3}
        \blindtext

        \mypara{ref4}
        \blindtext

    \end{multicols}
\end{document}
5
  • Have you considered to change your \label{#1} to also include something that will help you with easy identification? For example \label{par:#1}. This will help if you ever have a chapter and a paragraph with the same name, but need to reference both on separate occasions.
    – Flexo013
    Commented Jul 27, 2018 at 13:00
  • Thank you for this comment, I will consider doing that if I can get a viable fix to this problem to proceed with my document
    – tornadof3
    Commented Jul 27, 2018 at 13:16
  • Is there a particular reason that you are using \vfill? Since removing it results in this, which looks better IMO.
    – Flexo013
    Commented Jul 27, 2018 at 13:30
  • Thanks. I was only using vfill because a previous google suggestion for this custom paragraph function had the vfill in. I agree it looks better without the vfill. Although on my live document I still have the same overall problem!
    – tornadof3
    Commented Jul 27, 2018 at 13:34
  • Without \vfill put the whole stuff in a box, eg.g., a \parbox or a minipage, or to use other that an invisible unbreakable box, maybe you would like the tcolorbox package (that can numbered and referenced, among a lot of another features, it have an excellent manual) but personally I will use sections as in the Jhor's answers.
    – Fran
    Commented Oct 26, 2018 at 7:41

2 Answers 2

1

You can use the standard sectioning mechanism, suitably adapted.

\documentclass[10pt]{book}
\usepackage{multicol}
\setlength\columnsep{1cm}  % sets the distance between columns
\usepackage{blindtext}

\makeatletter
\newcounter{para}
\newcommand\mypara[1]{%
  \@startsection{para}{1}{\z@}%
    {-\topsep}%
    {\topsep}%
    {\normalfont\bfseries\centering}%
    {}\label{#1}%
}
\newcommand{\myparamark}[1]{}
\renewcommand{\@seccntformat}[1]{%
  \ifcsname format#1\endcsname
    \csname format#1\endcsname
  \else
    \csname the#1\endcsname\quad
  \fi
}
\newcommand{\formatpara}{\thepara}
\makeatother


\begin{document}

\title{Minimum working example}
\pagestyle{empty}

% main part of the document:

\mainmatter

\begin{multicols}{2}

\mypara{ref1}
%A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more\\
A line more

\blindtext

\mypara{ref2}
\blindtext

\mypara{ref3}
\blindtext

\mypara{ref4}
\blindtext

\end{multicols}

\end{document}

You can check that uncommenting the first A line more, section 4 will be pushed to the next page.

0

Removing the \vfill as pointed by @Flexo013 actually fixes your local problem. I would however provide another solution, that IMO is more efficient.

First, if you don use \chapter (and have no \frontmatter) you definitely don't need the features of the book class. Second, if you wish to be able to produce a TOC and bookmarks, your \para won't do the job.

Let me switch to article and modify the top-level sectioning command \section by the use of a sinle line \titleformat from titlesec package. The MWE :

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\setlength\columnsep{1cm}  % sets the distance between columns
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{hyperref}
\pagestyle{empty}

\begin{document}
\begin{multicols}{2}

\tableofcontents

\makeatletter
\titleformat{\section}[block]{\centering\bfseries}{\thesection}{0pt}{\@gobble{#1}}[\label{par:#1}]
\makeatother

    \section{foo}
    \blindtext

    \section{bar}      
    In \S\ref{par:foo} we said that \ldots

    \section{foo2}      
     \blindtext

    \section{bar2}      
    In §\,\ref{par:foo2} we will say that \ldots

    \section{foo3}      
    \blindtext

    \section{bar3}      
     \blindtext

    \end{multicols}
\end{document}
2
  • Thank you. I will try this and see how it works
    – tornadof3
    Commented Jul 27, 2018 at 17:59
  • In this answer I didn't really addressed the problem of pagebreak between title (in your case the number) and the body of the paragraph. In principle, the standard mechanism of LaTeX for handling the sectioning commend should avoid 90% of such breaks. If some still occur, one could tweak the penalties.
    – Jhor
    Commented Jul 27, 2018 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .