4

I like to create shortcuts for symbols I use often, and I want to change the behavior of the underscore following a custom command. For example, if I want to add a subscript, I want to change the placement of the subscript, but a subscript proceeding a command will just place it at the end. This is a minimal example of the behavior I would like to achieve.

\documentclass{article}

\newcommand{\fx}{{f(x)}}
\newcommand{\fxi}{{f_i(x)}}

\begin{document}
This is a function: $\fx$. This is the $i$th function: $\fxi$. 
I want to do this $\fx_i$ but make it look like this $\fxi$.
\end{document}

Example

2
  • I’d simply use f_i(x), which is semantically sound and not more difficult to type
    – egreg
    Dec 1, 2022 at 23:01
  • @egreg I agree. This is a simplified example to illustrate the question. I will use this for more complex expressions.
    – Ralff
    Dec 2, 2022 at 2:37

3 Answers 3

4

you can use the xparse commands:

\documentclass{article}

\newcommand{\fxi}{{f_i(x)}}
\NewDocumentCommand\fx{e{_}}
 {
   \IfNoValueTF{#1}
    {f(x)}
    {f_{#1}(x)}
 }
\begin{document}
This is a function: $\fx$. This is the $i$th function: $\fxi$. I want to do 
this $\fx_i$ but make it look like this $\fxi$. 
\end{document}

enter image description here

2
  • Thanks! This is what I was imagining. What is the e for in the new document command?
    – Ralff
    Dec 1, 2022 at 21:04
  • 1
    @Ralff texdoc xparse → "e Given as e{⟨tokens⟩}, a set of optional embellishments, each of which requires a value. If an embellishment is not present, -NoValue- is returned. Each em- bellishment gives one argument, ordered as for the list of ⟨tokens⟩ in the argument specification. All ⟨tokens⟩ must be distinct. This is an experimental type."
    – user202729
    Dec 2, 2022 at 3:41
6

The following solution uses only TeX primitives. Using \futurelet you can test if the next character is _.

\def\fx{\futurelet\next\fxA}
\def\fxA{\ifx\next_\expandafter\fxB\else f(x)\fi}
\def\fxB_#1{f_{#1}(x)}

\def\fxi{f_i(x)}

This is a function: $\fx$. This is the $i$th function: $\fxi$. 
I want to do this $\fx_i$ but make it look like this $\fxi$.
2
  • Thanks! What is the advantage of using only tex primitives in contrast to new command?
    – Ralff
    Dec 2, 2022 at 2:38
  • 1
    @Ralff (if I understood correctly, not putting words in wipet's mouth) personal preference. For simplicity, so the user can understand the inner working. Maybe read petr.olsak.net/opmac-e.html for some more information.
    – user202729
    Dec 2, 2022 at 3:44
2

Another possibility: Define a macro called, say, \fx, which takes a single, optional argument that's empty by default.

enter image description here

\documentclass{article}
\newcommand{\fx}[1][]{f_{#1}(x)} % 1 optional argument; default: no arg.

\begin{document}
A basic function:   $\fx$

The $i$th function: $\fx[i]$

The $n$th function: $\fx[n]$
\end{document}
1
  • Thanks! This would solve my problem although I prefer modifying the underscore operator directly (as the usage is more intuitive to me)
    – Ralff
    Dec 1, 2022 at 21:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .