GeneralWhat does \@gobble do?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
youthdoo
Posts: 1
Joined: Sun Dec 11, 2022 11:59 am

What does \@gobble do?

Post by youthdoo »

In macro2e, page 6, there's 3.2 Expanding/Gobbling Arguments, and in which you can see a macro called \@gobble. It's description says Removes (gobbles) its argument. (long) but I still don't know what it does.
I'm asking because I'm recently going through this list of macros and I have trouble understanding this one.
What's this...

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

What does \@gobble do?

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

\@gobble takes an argument and does nothing. \@gobbletwo takes two arguments and does nothing. Sounds meaningless, but for example it's a way to disable a command that takes arguments. You can override or disable simple commands like \let\thepage\relax. (\relax also does "nothing") Now, you don't have page numbers displayed anymore. But if you do the same with macros with arguments, the arguments would still be there and appear in your text. Then you can use \gobble, \@gobble or \@gobbletwo.

Quick examples:

Here, we disable page numbering:

Code: Select all

\documentclass{article}
\pagenumbering{gobble}
\begin{document}
First page
\newpage
Second page
\newpage
Third page
\end{document}
Here, we disable the \chapter macro because we don't want to have a separate chapter for the list of figures:

Code: Select all

\documentclass{book}
\makeatletter
\let\chapter\@gobbletwo
\makeatother
\begin{document}
text
\listoffigures
more Text
\end{document}
Stefan
LaTeX.org admin
User avatar
MjK
Posts: 89
Joined: Fri Jan 28, 2022 6:09 pm

What does \@gobble do?

Post by MjK »

Stefan Kottwitz wrote: Here, we disable the \chapter macro because we don't want to have a separate chapter for the list of figures:

Code: Select all

\documentclass{book}
\makeatletter
\let\chapter\@gobbletwo
\makeatother
\begin{document}
text
\listoffigures
more Text
\end{document}
IMHO not the best example, at least not without any clarification. Usually \chapter is a command with an optional and a mandatory argument, but this does not fit \@gobbletwo, because it eats two mandatory arguments. So why does it work in the example? It works, because \listoffigures does use \chapter*{\listfigurename}. So in the example, the asterisk * is the first argument and {\listfigurename} is the second. However, the shown code would fail using also a normal chapter:

Code: Select all

\documentclass{book}
\makeatletter
\let\chapter\@gobbletwo
\makeatother
\begin{document}
\listoffigures
\chapter{Test}
With text.
\end{document}
Here it eats also the W of the word With after \chapter{Test} because now, \chapter is a command with two mandatory arguments (and both are not used). An example like

Code: Select all

\documentclass{book}
\makeatletter
\let\chapter\@gobbletwo
\makeatother
\begin{document}
\listoffigures
\chapter[Toas]{Test}
With text.
\end{document}
Would also fail. So a IMHO somehow better example would be:

Code: Select all

\documentclass{book}

\begin{document}
text
\begingroup
\makeatletter
\let\chapter\@gobbletwo
\listoffigures
\endgroup
more Text
\end{document}
But also only with the explanation above.
My main topics are KOMA-Script and other questions related to my packages. If I reply to any other topic or if you've not yet added a minimal working example, please do not expect any further response. And please don't forget to tag examples as code.
Post Reply