Page LayoutUseful Function - Help Me Understand

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Post by LaTexLearner »

I am creating a document that automatically starts each page for each section. I've been using this code which has worked really well, but I would like to understand why it works so that I can apply the general idea to all my future code-writing.

Code: Select all


\let\stdsection\section
\renewcommand\section{\newpage\stdsection}

What does all that stuff mean? :)

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Useful Function - Help Me Understand

Post by Johannes_B »

I wouldn't call this good coding. What you have there is more or less a standard hack.

First, with let you copy the definition of section to stdsection. Later, section is redefined to use a newpage, and later use the original definition of section, that is stored in stdsection.

On a sidenote, if you want that every section starts on a new page, it is more likely that you want to have a document class like report or book. Articles for shorter stuff, reports for longer reports; well, and books for books ;-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Post by LaTexLearner »

Johannes_B wrote:First, with let you copy the definition of section to stdsection. Later, section is redefined to use a newpage, and later use the original definition of section, that is stored in stdsection.
I think I understood that:

You're saying that the first line "turns" \stdsection into the same thing as \section, i.e. \stdsection = \section, but normally \stdsection ≠ \section

Then on the second line, \section is redefined in a way that incorporates what \section used to be PLUS the page break.

In other words I could also have written:

Code: Select all

\let\jinglebells\section
\renewcommand\section{\newpage\jinglebells}
and it would have had the same effect?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Post by LaTexLearner »

Johannes_B wrote: On a sidenote, if you want that every section starts on a new page, it is more likely that you want to have a document class like report or book. Articles for shorter stuff, reports for longer reports; well, and books for books ;-)
I went to these websites to read about books vs reports vs articles. It didn't seem like there was THAT big of a difference... or am I missing something?

http://tex.stackexchange.com/questions/ ... re-the-mai

http://texblog.org/2007/07/09/documentc ... or-letter/
User avatar
Stefan Kottwitz
Site Admin
Posts: 10343
Joined: Mon Mar 10, 2008 9:44 pm

Useful Function - Help Me Understand

Post by Stefan Kottwitz »

LaTexLearner wrote:and it would have had the same effect?
Yes, it would. We got two lines for two reasons:
  • To a avoid infinite recursion: \renewcommand\section{\newpage\section} could replace in itself endlessly, or until memory is gone.
  • To save the original command for using later, or for switching back by \let\section\stdsection after such a hack. \stdsection was just a new macro name we introduced for this purpose. We could have chosen any name.
Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10343
Joined: Mon Mar 10, 2008 9:44 pm

Useful Function - Help Me Understand

Post by Stefan Kottwitz »

LaTexLearner wrote:about books vs reports vs articles. It didn't seem like there was THAT big of a difference... or am I missing something?
They have a similar base. But the layout of articles is different to books/reports: the main difference is the support for chapters, wich has some consequences. Unlike sections, chapters have special headings, alway have a page break before, start by default on right hand pages, counters are usually reset per chapter, floating figures stay in their chapter etc.

So, no chapters - choose article. You need chapters - choose book or report. Between book and report theres not a big difference indeed, it's more that the default options are different. A classic book is printed two-sided, start chapters always on odd pages (right hand pages), so they insert a blank page if necessary. They justify page content so the height is equal, which looks better in two-sided printing. Then the book class offers partitioning in parts with different numbering style. Plus some small differences.

You choose what fits best do the desired design (journal article, comprehensive book or medium report) and adjust according to your needs. The classes offer basic functionality, some meaningful defaults, and ways to customize. KOMA-Script classes are somehow equivalent but much more capable and customizable.

Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Useful Function - Help Me Understand

Post by Johannes_B »

LaTexLearner wrote:In other words I could also have written:

Code: Select all

\let\jinglebells\section
\renewcommand\section{\newpage\jinglebells}
and it would have had the same effect?

Yes, you are creating a new command called \jinglebells, in which the current definition of section is copied. Later, section is redefined, issuing a newpage and the very original definition that was copied into jinglebells.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Post by LaTexLearner »

So why doesn't this macro work?

I would like to redefine the \tabluarx command so that it is always preceded by \noindent.

This just gives me error messages, including one that says I haven't written "\begin{document}" even when I clearly have. Is it not possible to renew commands with arguments in them?

Code: Select all

\let\jinglebells\begin{tabularx}
\renewcommand\begin{tabularx}{noindent\begin{\jinglebells}}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Useful Function - Help Me Understand

Post by Johannes_B »

You are using it wrong ;-)

Code: Select all

\documentclass{article}
\usepackage{showframe}
\usepackage{tabularx}
\let\jellyfish\tabularx
\renewcommand{\tabularx}{\noindent\jellyfish}
\begin{document}
\begin{tabularx}{\linewidth}{X}
	\hrule
\end{tabularx}
\end{document}

But better in any way: Use modern tools instead of quick hacks.

Code: Select all

\documentclass{article}
\usepackage{showframe}
\usepackage{tabularx}
\usepackage{etoolbox}
%\tracingpatches
\pretocmd{\tabularx}{\noindent}{}{}
\begin{document}
\begin{tabularx}{\linewidth}{X}
	\hrule
\end{tabularx}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Post by LaTexLearner »

Johannes_B wrote:
...

But better in any way: Use modern tools instead of quick hacks.

Code: Select all

\documentclass{article}
\usepackage{showframe}
\usepackage{tabularx}
\usepackage{etoolbox}
%\tracingpatches
\pretocmd{\tabularx}{\noindent}{}{}
\begin{document}
\begin{tabularx}{\linewidth}{X}
	\hrule
\end{tabularx}
\end{document}
What makes this way better? The extra package seems to make it more complicated to me.
Post Reply