Text FormattingIs there a way to get different outputs from \newcommand?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
nikio
Posts: 8
Joined: Thu Jan 25, 2024 10:26 am

Is there a way to get different outputs from \newcommand?

Post by nikio »

I'd like to have a command with optional input that does the following:
if there is no input the output would be A_{ab}
if I input [cd] the output would be A_{ab to cd}
At the moment I use \newcommand like this
\newcommand{\A}[1][]{\ensuremath{A_{ab #1}}}
but in this way I have to input "to cd" instead of "cd"... not a big deal but when I'll have to insert something more complex it will become annoying

Is there a way to make \newcommand do it or some better alternative that someone knows about?

Code: Select all

\documentclass[]{article}
\newcommand{\A}[1][]{\ensuremath{A_{ab #1}}}

\begin{document}
	
this is how my command works

\verb*|$\A$| $\rightarrow$	$\A$

\verb*|$\A[cd]$| $\rightarrow$	$\A[cd]$

\verb*|$\A[, cd]$| $\rightarrow$	$\A[, cd]$\\

but I'd like this:

\verb*|$\A[cd]$| $\rightarrow$	$\A[, cd]$
	
\end{document}
Last edited by nikio on Thu Feb 01, 2024 12:56 am, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Bartman
Posts: 366
Joined: Fri Jan 03, 2020 2:39 pm

Is there a way to get different outputs from \newcommand?

Post by Bartman »

You can use the \IfNoValueTF command or its reverse form (ltnews32) to check whether there is an optional argument or not.

Code: Select all

\documentclass{article}

\newcommand{\A}[1][]{%
  \IfNoValueTF{#1}
    {\ensuremath{A_{ab}}}
    {\ensuremath{A_{ab,#1}}}
}

\begin{document}
\verb*|$\A$| $\rightarrow\A$

\verb*|$\A[cd]$| $\rightarrow\A[cd]$
\end{document}
nikio
Posts: 8
Joined: Thu Jan 25, 2024 10:26 am

Is there a way to get different outputs from \newcommand?

Post by nikio »

I think I'm missing something: your reply seems logically what should do the job but doesn't work as I expected, in the No Value option the second option is executed with no value,
I changed a little the code to make it clearer I hope:

Code: Select all

\documentclass{article}

\newcommand{\F}[1][]{
	\IfNoValueTF{#1}
	{\ensuremath{F_{A}}}
	{\ensuremath{F_{A\rightarrow #1}}}
}

\begin{document}
	
	\verb*|$\F$| outputs $\F$ \qquad instead of $F_A$ with no arrow
	
	\verb*|$\F[B]$| outputs $\F[B]$ \qquad OK
\end{document}
nikio
Posts: 8
Joined: Thu Jan 25, 2024 10:26 am

Is there a way to get different outputs from \newcommand?

Post by nikio »

I think I found the problem. If no argument is inserted, an empty value is returned which is different from a "NoValue" value, therefore \IfNoValueTF returns always false.

I found a couple of soutions to deal with the problem:

The first does not require packages but uses some somewhat cryptic Tex code:

Code: Select all

\documentclass{article}
\newcommand{\F}[1][]{
	{
		\ifx&#1&
		\ensuremath{F_{A}}
		\else
		\ensuremath{F_{A\rightarrow #1}}
		\fi}
	}

\begin{document}
	\verb*|$\F$| outputs $\F$ \qquad OK
	
	\verb*|$\F[B]$| outputs $\F[B]$ \qquad OK
\end{document}
The second is quite straightforward but needs a supplementary package:

Code: Select all

\documentclass{article}
\usepackage{ifthen}
\newcommand{\F}[1][]{
		\ifthenelse{\equal{#1}{}}
		{\ensuremath{F_{A}}}
		{\ensuremath{F_{A\rightarrow #1}}}
}

\begin{document}
	\verb*|$\F$| outputs $\F$ \qquad OK
	
	\verb*|$\F[B]$| outputs $\F[B]$ \qquad OK
\end{document}
Thank you Bartman for pointing me in the right direction by suggesting to insert a condition in the execution of the \newcommand
Bartman
Posts: 366
Joined: Fri Jan 03, 2020 2:39 pm

Is there a way to get different outputs from \newcommand?

Post by Bartman »

Sorry for my carelessness. I overlooked the narrow comma in the output and mistakenly assumed that in this case you could ignore the difference to the replacement commands for \newcommand.

Code: Select all

\documentclass{article}

\NewDocumentCommand{\F}{o}{
  \IfNoValueTF{#1}
    {\ensuremath{F_{A}}}
    {\ensuremath{F_{A\rightarrow #1}}}
}

\begin{document}
\verb*|$\F$| outputs $\F$
	
\verb*|$\F[B]$| outputs $\F[B]$
\end{document}
nikio
Posts: 8
Joined: Thu Jan 25, 2024 10:26 am

Is there a way to get different outputs from \newcommand?

Post by nikio »

Thank you, exactly what I was looking for, I'll have to have a better look into this \NewDocumentCommand!
Post Reply