10

I am writing a paper about functional programming where function application is denoted by space (e.g., f x y for f(x,y)). What would be the correct way to typeset that in LaTeX to get the correct spacing?

I know that for one-argument function one can use mathop as follow

\newcommand\foo{\mathop{\mathrm{foo}}}

but I wondered how to generalize it.

In particular $\foo x~y$ and $\foo x\,y$ don't seem to give correct spacing.

1
  • 1
    $\foo x\,y looks right to me, \, thinspace matches the space from \mathop Commented Mar 14, 2023 at 0:13

2 Answers 2

7

I suggest to use a command with two arguments: the function name and the list of arguments. This way you can easily change your mind about the notation (see comments in the code).

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\fun}{mm}
 {% #1 = function name
  % #2 = arguments, comma separated
  \operatorname{\mathsf{#1}}% style as you like
  % first possibility
  \,\clist_use:nn { #2 } { \,\, }% no parentheses and space between variables
  % second possibility
  %\clist_use:nn { #2 } { \, }% no parentheses and space between variables
  % third possibility
  %(\clist_use:nn { #2 } { , })% standard notation
 }

\ExplSyntaxOff

\begin{document}

\[
\fun{foo}{x,y} + \fun{baz}{x,y,z}
\]

\end{document}

enter image description here

I went for a doubled thin space as the thin space seems too small: here's the comparison when the second possibility is used:

enter image description here

For completeness, here's the third possibility:

enter image description here

You just change the definition, not the code inside the document.

2
  • +1, but I really, really hate not having the parentheses. I don't know whether foo x y + baz x y z is foo(x,y) + baz(x,y,z) or foo(x, y+baz(x,y,z)), foo(x, y+baz(x,y), z), foo(x, y+ baz(x), y, z), etc. I know parsers can figure this out, so there's a well-defined order of operations, but for me, personally, it adds to my mental load. (Yes, I know it's a common style, which is why I have such hate for it.) Commented Mar 14, 2023 at 20:26
  • 1
    @BenHocking I agree, but some people don’t 😊😏
    – egreg
    Commented Mar 14, 2023 at 23:18
6

In source listings and pseudocode, it’s customary to typeset function names in a typewriter font. (Although you might prefer a different style, such as sans-serif or small caps.) This often extends to spacing, but if you really want to make this easy to read, I think you want the function and its arguments to be spaced a little closer than binary operators, which are spaced a little more closely than relational operators, to match the order of operations.

So, maybe something like:

\documentclass{article}
\usepackage{amsmath}
\usepackage{inconsolata} % Or your monospace font of choice.

\newcommand\foo[2]{%
\textup{\texttt{foo}}\,\textup{\texttt{#1}}\,\textup{\texttt{#2}}%
}

\begin{document}
\[ \foo{x}{y} + \foo{y}{z} \geq \foo{x}{z}
\]
\end{document}

Inconsolata sample

You can gussy this up still more, of course. For example, this style sort of imitates DEK’s Concrete Math:

\documentclass{article}
\usepackage{amsmath}
\usepackage{inconsolata} % Or your monospace font of choice.
\usepackage{eulervm}

\newcommand\foo[2]{%
\textup{\texttt{foo}}\,{#1}\,{#2}%
}

\begin{document}
\[ \foo{x}{y} + \foo{y}{z} \geq \foo{x}{z}
\]
\end{document}

Inconsolata/Euler sample

If you declare more than one or two of these, you want some more generic command to declare an arbitrary source function.

2
  • Thank you very much, I really like the second option, can you give me a hint about the generic command you mentioned? Thank you
    – user292932
    Commented Mar 14, 2023 at 11:09
  • @John it looks like egreg wrote one.
    – Davislor
    Commented Mar 14, 2023 at 18:51

You must log in to answer this question.