TransWikia.com

Checking if section body was empty

TeX - LaTeX Asked on September 3, 2021

I want to create a command similar to @startsection from LaTeX kernel. I need to start new page if the previous body had something and not do it if it was empty.

I looked into kernel documentation, but I don’t understand how @noskipsec works.

MWE:

documentclass{article}

usepackage{etoolbox}

newtoggle{emptybody}
settoggle{emptybody}{true}

newcommand{mytitle}[1]{%
    iftoggle{emptybody}{textbf{#1}}{clearpage textbf{#1}}par
}

begin{document}

    mytitle{First title}
    
    mytitle{Should be on the same page}
    
    Text
    
    mytitle{Should be on the new page}

end{document}

Red line marks where pagebreak should appearenter image description here

One Answer

documentclass{article}

usepackage{etoolbox}

makeatletter
newcommand{mytitle}[1]{%
    ifbool{@nobreak}{textbf{#1}}{clearpage textbf{#1}}parnobreak@afterheading
}
makeatother
begin{document}

    mytitle{First title}

    mytitle{Should be on the same page}

    Text

    mytitle{Should be on the new page}

end{document}

Correct answer by Ulrike Fischer on September 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP