# Optional Argument New Command

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.

\documentclass[11pt]{article} % use larger type; default would be 10pt

\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)

%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
% \geometry{margin=2in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
%   read geometry.pdf for detailed page layout information

\usepackage{graphicx} % support the \includegraphics command and options

% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
\usepackage{amsmath} % allow the usage of math symbols, and tools
% These packages are all incorporated in the memoir class to one degree or another...

\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!

%%% END Article customizations

%%% The "real" document content comes below...

\title{Differential Equations}
\author{Jose M. Serra Jr.}
%\date{} % Activate to display a given date or no date (if empty),
% otherwise the current date is printed

%%Newcommands to ease the coding of the coding
\newcommand{\formula}[3]{
\ifx \relax[1]\relax
$$#1$$
\ifthenelse \relax[2]\relax
$$#1$$ \par
$$#2$$
\else
$$#1$$ \par
$$#2$$ \par
$$#3$$
\fi
}

\begin{document}
\maketitle
\textbf{Series Solutions of Linear Equations}
\textbf{Review of Power Series}

\formula{f(x)}

\end{document}
! Missing $inserted. <inserted text>$
l.83


I have a question pertaining to what is wrong with my new command? I read a post on the site, and they did something similar, so I tried doing one of my own, and it is not working.

• \ifx \relax[ compares \relax and [ so it is always false, but you do not say what you want this code to do, nor provide a compete test file that shows any behaviour so it is hard to answer your question Apr 22 '20 at 14:23
• also apart from the \ifx syntax this code appears to be inserting \par before display math should never start a paragraph TeX will generate a spurious empty first line of a paragraph above the display in that case. Apr 22 '20 at 14:25
• Ok ok hold on I am gonna upload the section Apr 22 '20 at 14:25
• why not simply use \begin{gather} eqn1 \\ eqn2 \\ eqn3 \end{gather} why do you need any tests? Apr 22 '20 at 14:27
• Why don't you say that in the question? but the standard syntax \begin{gather} aaa\\ bbb\\ccc \\ddd\end{gather} is much simpler and will handle any number of equations and does not force spurious paragraphs between the equations. So could answer what is wrong with the test but the actual use case should have no tests at all. Apr 22 '20 at 14:37

The test

\ifx \relax[


tests the two tokens \relax and [ and as these are not equal it is always false.

Presumably you intended

\ifx\relax#1\relax


which is better written as

\if\relax\detokenize{#1}\relax


which tests if #1 is empty (as if it is empty the two \relax tokens are compared, and equal)

But that would allow a syntax such as

\formula{a=b}{}{}


the macro \formula would always have three arguments, You are just testing if the arguments are empty ({}) . So in

\formula{f(x)}

\end


The three arguments are f(x), \par (from the blank line) and \end (intended for \end{document}).

Apart from these issues you are forcing \par before but this always makes poor spacing.

It is better to use a multiline display, so allowing any number of lines with no tests.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

one
$$eq:a=b$$

two
\begin{gather}
a=b\\
c=5
\end{gather}

lots
\begin{gather}
a=bb\\x=y\\f=u\\77=77\\aaa=bbb
\end{gather}
\end{document}

• stackoverflow.com/questions/2144176/… Do you think you can help me doing it this way? Apr 22 '20 at 14:50
• LaTeX optional arguments should use [...] not {...} (the xparse package makes it easy to define commands with optional arguments) but there is zero benefit to using such a syntax here, you restrict the number of lines supported and hide the latex syntax so will lose syntax highlighting and context sensitive support in your editor, and make debugging any errors in the math markup a lot harder.. Apr 22 '20 at 14:53