7

In this question the given argument wasn't used, because #1 was missing..

One would actually assume an error or a warning be thrown.

A little MWE based on 1:

\documentclass{article}
\usepackage{listings}

    \lstnewenvironment{chat}[1][]{%
      \renewcommand{\lstlistingname}{Chat}%
      \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
      \lstset{
      backgroundcolor=\color{chatcolor}, #1 %<<-- if #1 is missing no warning or error is thrown.
    }}{}

\begin{document}
\begin{chat}[caption={Talking...}]
user12: Hello World!
\end{chat}

\end{document}
  • If a not used argument would return an error, you would need to have something like \discard{#1} because you don't always want to process all arguments. – Johannes_B Apr 4 '16 at 13:24
  • Also, #1 is specified as being optional with a default value of \@empty. If you remove the [] it will become a required argument. – John Kormylo Apr 4 '16 at 18:24
  • @JohnKormylo don't I have to specify a default value for it then? – naphaneal Apr 4 '16 at 18:32
  • Required arguments do not have default values. That is why they are required. – John Kormylo Apr 4 '16 at 18:34
  • @John yet even an argument is required, it is not per se said optional arguments are throwing a warning if their use is missing? – naphaneal Apr 4 '16 at 18:52
4

It's a feature of the TeX language: you're free to do whatever you want with the arguments to a macro, when defining it, including discarding them. Possibly the most important class of examples is that of macros like \@ifundefined: its definition is (approximately)

\newcommand{\@ifundefined}[3]{%
  \expandafter\ifx\csname #1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#2}{#3}%
}

(the real definition is a bit different, but not in an essential way). This macro relies on \@firstoftwo and \@secondoftwo:

\newcommand{\@firstoftwo}[2]{#1}
\newcommand{\@secondoftwo}[2]{#2}

As you see, the purpose of both is exactly discarding one of their arguments. When calling \@ifundefined{abc}{def}{ghi}, if the conditional returns true (for \abc being undefined or equivalent to \relax), TeX will find

\@firstoftwo{def}{ghi}

in the input stream, whereas it will find \@secondoftwo{def}{ghi} if the conditional returns false (for \abc having some meaning different from \relax).

I wouldn't like to be warned each time \@firstoftwo is used, but wouldn't like to be warned when it is defined either, because my aim is exactly defining \@firstoftwo to discard one of its arguments.

Besides, macro definitions can change at any moment; it's not at all uncommon that in certain places a one parameter macro is locally redefined to do nothing with its argument. Look for \@gobble in latex.ltx to see several examples.

One of the differences between TeX and other programming languages is that TeX aims primarily at processing text. Macro definitions, unlike functions and routines in other languages, are not the main aspect of TeX. When you do \def, TeX simply stores the (parameter text and the) replacement text in memory, without any kind of optimization or streamlining. The only check is that no more than the stated arguments are used (and that braces are balanced, ).

In a compiled language, it's quite likely that you get a warning if you define two functions to do exactly the same thing or if a function doesn't use one of the variables it's required to be supplied with, because the compiler is able to do deep lexical, syntactic and semantic analysis. However, even if we informally talk about “compiling a LaTeX file”, TeX is an interpreter, not a compiler.

Gobbling input is normally done by interpreted languages. You get no warning if you define a function in Perl or Lua which doesn't use all of its arguments: it could be a “one shot” or even a general function aimed at discarding part of the input under certain circumstances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.