可选参数新命令

可选参数新命令
% !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...

%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

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

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\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
    \begin{equation} #1 \end{equation}
\ifthenelse \relax[2]\relax
    \begin{equation} #1 \end{equation} \par
    \begin{equation} #2 \end{equation}
\else
    \begin{equation} #1 \end{equation} \par
    \begin{equation} #2 \end{equation} \par
    \begin{equation} #3 \end{equation}
\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 

我有个问题,我的新命令有什么问题?我读了网站上的一篇文章,他们做了类似的事情,所以我试着做了一个自己的命令,但没有用。

答案1

考试

\ifx \relax[

测试两个标记\relax[并且由于它们不相等,所以它始终为假。

你可能打算

\ifx\relax#1\relax

更好的写法是

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

测试是否#1为空(如果为空,则\relax比较两个标记,结果相等)

但这将允许如下语法

\formula{a=b}{}{}

\formula总是有三个参数,你只是测试参数是否为空({})。所以在

\formula{f(x)}

\end

这三个参数分别是f(x)\par(来自空白行)和\end(用于\end{document})。

除了这些问题之外,您\par之前一直在强制解决\begin{equation},但这总是会导致间距不佳。

最好使用多行显示,这样允许任意数量的行无需测试。

\documentclass{article}

\usepackage{amsmath}

\begin{document}

one
\begin{equation}
  eq:a=b  
\end{equation}

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}

相关内容