0

In my simple example I try to decompose formatting of the text into multiple macro definitions. I use xelatex of miktex disribution to compile it into a pdf.

The question is why does the second %FORM 2 definition doesn't work whereas the %FORM 1 works just fine.

\documentclass{article}

\def\transformrawinput#1,#2,#3,#4 {\textit{#1} \textbf{#2} \underline{#3} #4} %FORM 1 %\def\finaltransfmormations{\transformrawinput\firstword,\secondword,\thirdword,\forthword \ \rule{\textwidth}{1pt}} %FORM 2 \def\finaltransfmormations{\transformrawinput{\firstword}{\secondword}{\thirdword}{\forthword} \ \rule{\textwidth}{1pt}}

\def\firstword{one} \def\secondword{two} \def\thirdword{four} \def\forthword{five}

\begin{document} \finaltransfmormations \end{document}

Cheers!

Runaway argument?
{\firstword }{\secondword }{\thirdword }{\forthword } \\ \rule {\textwidth \ETC
.
! File ended while scanning use of \transformrawinput.
<inserted text>
                \par
<*> ./test_edef_def.tex

?

enter image description here

avenir
  • 103
  • 1
    If you delimit the macro arguments using , in the definition, you need to also do so when calling it. – schtandard Sep 18 '22 at 10:15
  • @schtandard I am just starting to learn the tex's marcros. I have been reading the introduction on here https://en.wikibooks.org/wiki/LaTeX/Plain_TeX, where they explicitly say that the %FORM 2 as in my example is perimitted and encouraged ("If you do not want to play with separators, then Plain TeX macros are used just as LaTeX macros (without default parameter):" and then they show an example). Does that mean that the tutorial is incorrect? – avenir Sep 18 '22 at 10:25
  • do not use \def when defining latex commands and this issue will not arise. \def is a tex primitive low level definition command and not intended for use in latex (it is not even mentioned in the latex book) – David Carlisle Sep 18 '22 at 10:27
  • @avenir in general the wikibook is not always very acurate but here it says, as you quote, "If you do not want to play with separators" so it is discussing the case without separators ie without the comma and space tokens you are using in #1,#2,#3,#4 – David Carlisle Sep 18 '22 at 10:33
  • No, the tutorial isn't technically incorrect, it is just not the right tutorial for you as you probably don't really want to learn Plain TeX but rather LaTeX. In order for your Form 2 to work, you need to remove the delimiters in you definition (\def\transformrawinput#1#2#3#4{) but as David mentioned, you are probably better off just using LaTeX commands like \newcommand or \NewDocumentCommand. – schtandard Sep 18 '22 at 10:35
  • @schtandard Thank you, that was the answer, that is what I was looking for. The comas in the initial definition were what made the compiler complain for the %FORM 2 call. Thanks! – avenir Sep 18 '22 at 10:39

2 Answers2

1

Consolidating some comments together:

The following would be valid TeX:

\def\withcomma#1,#2{:#1:#2:}
\def\withoutcomma#1#2{:#1:#2:}

\withcomma one,two

\withoutcomma{one}{two}

\bye

with the output

:one:t:wo
:one:two:

Your \def\transformrawinput is specifying that its four arguments will be separated by commas, which is what FORM 1 uses. If you want to specify your arguments with braces, then you wouldn't want the commas to appear in the macro definition. But there's a problem with your comma approach: because you never give a closing delimiter, TeX takes the next token to be the last argument. This turns out to be the first letter of what you thought to be the last argument. This means that it should really be called \withcomma one,{two}. But if you have to surround the argument with braces anyway, then you might as well drop the commas.

But you said you're using (Xe)LaTeX, not XeTeX. In that case, \def works because LaTeX is built on top of TeX, but you should use the LaTeX approach of \newcommand{\withoutcomma}[2]{:#1:#2:} or \NewDocumentCommand{\withoutcomma}{mm}{:#1:#2:} (which might \usepackage{xparse}), so that you would end up with

\documentclass{article}
%\newcommand{\withoutcomma}[2]{:#1:#2:} % or
\usepackage{xparse} % for TeX prior to 2022
\NewDocumentCommand{\withoutcomma}{mm}{:#1:#2:}
\begin{document}
\withoutcomma{one}{two}
\end{document}
Teepeemm
  • 6,708
0

It is worth noting that the usage of \def macros is highly discouraged in LaTeX and its usage comes with risks.

To the question of the correct syntax for the definition of the macro for the call of the %FORM 2, it is the following (all credit goes to @schtandard for his answers in the comments):

\def\transformrawinput#1#2#3#4{
avenir
  • 103
  • but that is incorrect syntax for latex, it should be \newcommand\transformrawinput[4]{ – David Carlisle Sep 18 '22 at 13:36
  • Thank you for your remark @david-carlisle. I completely understand your point. From that perspective the entire question was ill formulated, because \def is not to be used in latex. Though regarding the situation where its use could be potentially justified the question of the syntax has its own right even within latex. Thanks to my background in C programming language I find it more transparent to reason about Macros, than in terms of higher level newcommand constructs that could have side effects that are unknown for a new user and that require additional study. – avenir Sep 19 '22 at 05:12
  • no, a new latex user using official documentation wouldn't even know of \def. it's like saying a user of some high level c API is better to directly use raw pointer manipulation rather than trust the functions of the API. it's syntactically legal but leads you to untested and unsupportable code. saying to use def rather than newcommand to avoid further study has the situation backwards – David Carlisle Sep 19 '22 at 06:17
  • @david-carlisle, Thank you vrey much from your clarification. – avenir Sep 19 '22 at 13:51