Generalif, else, fi

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
x42bn6
Posts: 14
Joined: Tue Apr 07, 2009 1:35 am

if, else, fi

Post by x42bn6 »

Probably because they're such common terms, I can't find anything explaining how to get conditionals to work.

I have the following bit of code:

Code: Select all

\DeclareMathOperator{\fisherInformationOp}{\operatorname{I}}
\newcommand{\I}[2][]{\fisherInformationOp_#1\left(#2\right)}
The idea being that sometimes I want to subscript things but not all the time.

However, when there is only one argument, the subscript takes the token \left and won't compile as there's no partnering \right. So I want to do something like:

Code: Select all

\DeclareMathOperator{\fisherInformationOp}{\operatorname{I}}
\newcommand{\I}[2][]{
	% If there are two arguments then...
		\fisherInformationOp_#1\left(#2\right)
	% Otherwise...
		\fisherInformationOp\left(#2\right)
	% \fi
}
How would I fill in the comments?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

if, else, fi

Post by localghost »

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\DeclareMathOperator{\op}{Op}
\newcommand{\opind}[2][\relax]{\op_{#1}\left(#2\right)}

\begin{document}
  $\opind{x} \opind[1]{x}$
\end{document}

Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
x42bn6
Posts: 14
Joined: Tue Apr 07, 2009 1:35 am

Re: if, else, fi

Post by x42bn6 »

That works, thanks.

Just out curiosity, could I know:

- What \relax does?
- Any sort of documentation on \if, \ifx, \else, \fi and so on?
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

if, else, fi

Post by localghost »

x42bn6 wrote:[...] What \relax does? [...]
Nothing (just relax).
x42bn6 wrote:[...] Any sort of documentation on \if, \ifx, \else, \fi and so on?
source2e, »TeX by Topic«, »TeX for the Impatient«
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
Post Reply