LaTeX forum ⇒ 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: 138
Joined: Tue Mar 10, 2015 11:06 am

Useful Function - Help Me Understand

Postby LaTexLearner » Mon May 18, 2015 10:13 pm

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.

  1. \let\stdsection\section
  2. \renewcommand\section{\newpage\stdsection}
  3.  


What does all that stuff mean? :)

Thanks!

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 3584
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Mon May 18, 2015 10:37 pm

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: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Fri May 22, 2015 6:38 am

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:

  1. \let\jinglebells\section
  2. \renewcommand\section{\newpage\jinglebells}


and it would have had the same effect?

LaTexLearner
Posts: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Fri May 22, 2015 6:40 am

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: 8529
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Fri May 22, 2015 1:01 pm

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
Site admin

User avatar
Stefan Kottwitz
Site Admin
Posts: 8529
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Fri May 22, 2015 1:10 pm

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
Site admin

User avatar
Johannes_B
Site Moderator
Posts: 3584
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Fri May 22, 2015 1:12 pm

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

  1. \let\jinglebells\section
  2. \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: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Mon Aug 03, 2015 7:10 am

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?

  1. \let\jinglebells\begin{tabularx}
  2. \renewcommand\begin{tabularx}{noindent\begin{\jinglebells}}

User avatar
Johannes_B
Site Moderator
Posts: 3584
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Mon Aug 03, 2015 12:19 pm

You are using it wrong ;-)

  1. \documentclass{article}
  2. \usepackage{showframe}
  3. \usepackage{tabularx}
  4. \let\jellyfish\tabularx
  5. \renewcommand{\tabularx}{\noindent\jellyfish}
  6. \begin{document}
  7. \begin{tabularx}{\linewidth}{X}
  8. \hrule
  9. \end{tabularx}
  10. \end{document}



But better in any way: Use modern tools instead of quick hacks.
  1. \documentclass{article}
  2. \usepackage{showframe}
  3. \usepackage{tabularx}
  4. \usepackage{etoolbox}
  5. %\tracingpatches
  6. \pretocmd{\tabularx}{\noindent}{}{}
  7. \begin{document}
  8. \begin{tabularx}{\linewidth}{X}
  9. \hrule
  10. \end{tabularx}
  11. \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: 138
Joined: Tue Mar 10, 2015 11:06 am

Postby LaTexLearner » Wed Aug 12, 2015 12:30 am

Johannes_B wrote:
...

But better in any way: Use modern tools instead of quick hacks.
  1. \documentclass{article}
  2. \usepackage{showframe}
  3. \usepackage{tabularx}
  4. \usepackage{etoolbox}
  5. %\tracingpatches
  6. \pretocmd{\tabularx}{\noindent}{}{}
  7. \begin{document}
  8. \begin{tabularx}{\linewidth}{X}
  9. \hrule
  10. \end{tabularx}
  11. \end{document}


What makes this way better? The extra package seems to make it more complicated to me.


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 4 guests