Illegal Parameter Number in Definition of currentlabelname to Be Read Again
Documents produced with the commands you lot have learned up to this bespeak will look acceptable to a large audience. While they are not fancy-looking, they obey all the established rules of good typesetting, which will brand them easy to read and pleasant to await at. However, there are situations where LaTeX does not provide a command or environment that matches your needs, or the output produced by some existing command may not encounter your requirements.
In this chapter, we will try to give some hints on how to teach LaTeX new tricks and how to brand it produce output that looks different from what is provided by default.
LaTeX is a fairly loftier-level language compared to Plain TeX and thus is more limited. The next chapter will focus on Plain TeX and will explain advanced techniques for programming.
New commands Edit
To add your own commands, use the
\newcommand { \name }[num]{definition}
command. Basically, the command requires two arguments: the name of the command you desire to create (preceded by a backslash), and the definition of the command. Note that the control name can merely demand non be enclosed in braces, every bit yous like. The num argument in square brackets is optional and specifies the number of arguments the new command takes (upwardly to 9 are possible). If missing it defaults to 0, i.east. no argument immune.
The post-obit two examples should assist you to get the idea. The first example defines a new command called \wbal that volition print "The Wikibook about LaTeX". Such a control could come in handy if you had to write the title of this volume over and again.
\newcommand { \wbal }{The Wikibook about \LaTeX } This is ''\wbal'' \ldots {} ''\wbal'' | This is "The Wikibook about LaTeX" … "The Wikibook about LaTeX" |
The next example illustrates how to define a new command that takes one argument. The #1
tag gets replaced past the argument you specify. If y'all wanted to use more than ane argument, apply #2
and so on, these arguments are added in an extra set up of brackets.
\newcommand { \wbalsup }[i] { This is the Wikibook virtually LaTeX supported by #1} \newcommand { \wbalTwo }[2] { This is the Wikibook about LaTeX supported by #i and #2} % in the document body: \brainstorm {itemize} \item \wbalsup {Wikimedia} \item \wbalsup {lots of users!} \item \wbalTwo {John Doe}{Anthea Smith} \terminate {catalog} |
|
Name your new command \wbalTwo
and not \wbal2
as digits cannot be used to name macros — invalid characters will fault out at compile-time.
LaTeX will non let you to create a new command that would overwrite an existing one. But there is a special command in example you explicitly want this: \renewcommand
. Information technology uses the same syntax every bit the \newcommand
command.
In certain cases you might also want to use the \providecommand
command. Information technology works like \newcommand
, but if the command is already divers, LaTeX will silently ignore the new command.
With LaTex2e, it is also possible to add together a default parameter to a command with the following syntax:
\newcommand {name}[num][default]{definition}
If the default parameter of \newcommand
is present, then the outset of the number of arguments specified by num is optional with a default value of default; if absent, then all of the arguments are required.
\newcommand { \wbalTwo }[2][Wikimedia]{ This is the Wikibook about LaTeX supported by {#1} and {#2}!} % in the document torso: \begin {catalog} \item \wbalTwo {John Doe} \item \wbalTwo [lots of users] {John Doe} \end {itemize} |
|
- Note
- When the command is used with an explicit first parameter it is given enclosed with brackets (here "[lots of users]").
Here is a common instance: if you are writing a book well-nigh Mathematics and you have to use vectors, yous take to decide how they will look. There are several different standards, used in many books. If a is a vector, some people like to add an pointer over it ( ), other people write information technology underlined ( a ); another common version is to write it bold (a). Let united states assume you lot desire to write your vectors with an arrow over them; and so add together the post-obit line in your mystyle.sty.
\newcommand { \myvec }[one]{ \vec {#1}}
and write your vectors inside the new \myvec {...}
command. Y'all can call it as you wish, just yous'd better choose a short name because you will probably write it very oftentimes. Then, if you lot modify your mind and you want your vectors to look differently you lot just have to alter the definition of your \myvec {...}
. Use this approach whenever you can: this will salvage you a lot of fourth dimension and increase the consistency of your document.
DeclareRobustCommand Edit
Some commands are fragile, that is they fail in some environments. If a macro works in torso text but not in (for case) a figure caption, it's worth trying to replace the \newcommand { \MyCommand }...
declaration with \DeclareRobustCommand { \MyCommand }...
in the preamble. This is especially truthful for macros which, when expanded, produce text that is written to a .aux file.
New environments Edit
But as with the \newcommand
command, there is a command to create your own environments. The \newenvironment
command uses the post-obit syntax:
\newenvironment {name}[num][default]{before}{after}
Once more \newenvironment
can accept an optional argument. When the \begin {name}
command (which starts the surround) is encountered, the textile specified in the before argument is processed earlier the text in the environment gets processed. The material in the after statement gets candy when the \cease {name}
command (which ends the environment) is encountered.
The optional num and default arguments are used the same way as in the \newcommand
control. LaTeX makes sure that you lot exercise not define an environs that already exists. If you ever want to modify an existing surround, you can use the \renewenvironment
command. It uses the aforementioned syntax as the \newenvironment
command.
The example beneath illustrates the usage of the \newenvironment
control:
\newenvironment {king} { \dominion {1ex}{1ex} \hspace { \stretch {ane}} } { \hspace { \stretch {one}} \dominion {1ex}{1ex} } \brainstorm {king} My humble subjects \ldots \end {king} |
Edit
When creating a new environment you may easily get bitten past extra spaces creeping in, which tin potentially have fatal effects. One example is when you lot want to create a championship surroundings which suppresses its ain indentation as well as the one on the post-obit paragraph. The \ignorespaces
control in the begin block of the environment volition make it ignore any space subsequently executing the begin block. The finish block is a bit more tricky as special processing occurs at the end of an environment. With the \ignorespacesafterend
LaTeX will upshot an \ignorespaces
afterward the special 'end' processing has occurred.
\newenvironment {simple} % { \noindent } % { \par\noindent } \brainstorm {simple} Run into the space\\to the left. \end {simple} Same\\here. | See the infinite to the left. Same here. |
\newenvironment {correct} % { \noindent\ignorespaces } % { \par\noindent % \ignorespacesafterend } \brainstorm {correct} No space\\to the left. \end {correct} Same\\here. | No space to the left. Aforementioned hither. |
Also, if yous're still having problems with extra space being appended at the end of your environs when using the \input for external source, make sure there is no infinite betwixt the beginning, sourcing, and cease of the environment, such as:
\brainstorm {right} \input {somefile.tex} \cease {correct}
or
\brainstorm {right} % \input {somefile.tex} % \end {right}
Declare commands within new environs Edit
New commands tin can be declared within newenvironment. Commands declared within the newenvironment refer to their arguments by doubling the # character. In the following instance, a new environment is alleged along with a nested control:
\newenvironment {topics}{ \newcommand { \topic }[two]{ \item {##i / ##ii} } Topics: \begin {itemize} } { \finish {itemize} }
If, by mistake, the arguments passed to the \topics macro are defined with a single # character, the following error message will be thrown:
! Illegal parameter number in definition of \topics.
Extending the number of arguments Edit
The xkeyval packages will let you define central/value options for commands.
\mycommand [key1=value1, key3=value3] {some text}
The package is quite complete and documentation is exhaustive. Nosotros recommend that package developers read it. [1]
Let'southward provide a unproblematic example[i]:
\usepackage {xkeyval} % ... \makeatletter \def\my@emphstyle#1{ \csname my@mode@#1\endcsname } %% Predefined styles \providecommand\my@style@default{ \em } \providecommand\my@style@bold{ \bfseries } \define@key{myemph}{code}{ % \def\my@emphstyle{#1} } \define@key{myemph}{way}{ % \def\my@emphstyle{ \csname my@style@#ane\endcsname } } \newcommand\setemph [1] { % \setkeys {myemph}{#1} } \renewcommand\emph [1] { % { \my@emphstyle #i} } \makeatother Something \emph {important} \setemph {style=bold} Something \emph {important} \setemph {code=\Large\sffamily } Something \emph {important}
Arithmetic Edit
LaTeX tin can manipulate numbers.
The calc package provides the common infix notation.
\usepackage {calc} % ... \newcounter {mine} \setcounter {mine}{2*17} \themine
For high-precision computations, yous tin use the fp [two] package.
\usepackage {fp} % Clip \[ \FPmul\consequence { ii }{ 7 } \FPclip\issue\result 2 * 7 = \result \] % Infix \[ \newcommand\effect { 11 } \sqrt { \sin ( 2 + \result ) } \approx \FPeval\upshot {circular ( root ( 2 ,sin ( result + two . 5 )) , ii ) } \result \] % Postfix \[ \FPupn\result { 17 2 . 5 + 17 . 5 swap - 2 1 + * 2 swap / } % or \FPupn\consequence{2 17.v 17 2.5 + - 2 1 + * /} \FPclip\upshot\result ( 17 + ii . 5 - 17 . v ) * ( 2 + 1 ) / two = \result \] % High precision \[ \FPdiv\result { 17 }{ 7 } \frac { 17 }{ 7 } \approx \FPtrunc\result\result { iii } \upshot \]
Conditionals Edit
LaTeX can utilise conditionals thanks to the ifthen package.
\usepackage {ifthen} % ... \ifthenelse { \equal { \myvar }{true} }{ This is truthful. }{ This is false. }
FYI: For newer projects e-TeX is recommended[3], bachelor in LaTeX from etoolbox. See section 3.6 of their manual.
Post-obit a short example, a bool that switches its state every time it is used.[4]
\documentclass {article} \usepackage {etoolbox} \usepackage {parskip} \usepackage {tikz} \newbool {volt} \newcommand { \onoff }{ % \ifbool {volt}{ % \boolfalse {volt} \candleOn\space ON}{ % \booltrue {volt} \candleOff\space OFF} % } \newcommand { \candleOff }{ \tikz \describe (0,0) rectangle (.2,.8);} \newcommand { \candleOn }{ \begin {tikzpicture} \draw (0,0) rectangle (.2,.8);\draw [fill=orange] (.1,.9) circle [radius=.1];\end {tikzpicture} } \brainstorm {certificate} \onoff\par \onoff\par \onoff\par \onoff\par \finish {document}
Loops Edit
The PGF/TikZ extension provides the \foreach
command.
\usepackage {tikz} % ... \foreach \i/\q in {wheat/50g, water/1L, yeast/2g}{ \noindent\i\dotfill\q\\ }
If you are just using \foreach
and not drawing graphics, you may instead use the pgffor
package straight.
Alternatively y'all can check out the multido parcel.
Strings Edit
xstring provides a lot of features. From CTAN:
- testing a string'southward contents
- extracting substrings
- substitution of substrings
- string length
- position of a substring
- number of recurrences of a substring
Examples:
\usepackage {xstring} % ... \newcommand\mystr {Hello World!} The string ``\mystr'' has \StrLen { \mystr }{} characters. Predicate ``\mystr {} contains the word Hello'' is \IfSubStr { \mystr }{Howdy}{true}{false}.
LaTeX Hooks Edit
LaTeX provides two hooks:
-
\AtBeginDocument
volition let you specify a set of commands that will be executed when\begin {document}
is met. -
\AtEndDocument
does the same for\end {certificate}
.
This gives you some more than flexiblity for macros. Information technology tin can be useful to override settings that get executed after the preamble. These hooks can be called several times. The commands volition be executed in the club they were set.
For example, let's replace the page numbers with oldstylenums:
\usepackage {textcomp} \AtBeginDocument { % % Make the page numbers in text figures \let\myThePage\thepage \renewcommand { \thepage }{ \oldstylenums { \myThePage } } }
At that place are as well hooks for classes and packages. Run across Creating Packages.
Command-line LaTeX Edit
If you piece of work on a Unix-similar Bone, you might be using Makefiles or any kind of script to build your LaTeX projects. In that connection it might be interesting to produce unlike versions of the aforementioned document past calling LaTeX with command-line parameters. If you add the following structure to your document:
\usepackage {ifthen} %... % default value. \providecommand\blackandwhite {faux} %... \ifthenelse { \equal { \blackandwhite }{true} }{ % "black and white" mode; practise something.. }{ % "colour" mode; practise something dissimilar.. }
At present you lot tin can phone call LaTeX like this:
latex '\providecommand{\blackandwhite}{true}\input{test.tex}'
Beginning the command \blackandwhite
gets defined and then the actual file is read with input. Past setting \blackandwhite
to false the color version of the document would exist produced.
Notes and References Edit
- ↑ tex.stackexchange.com
- ↑ ctan.mackichan.com
- ↑ https://tex.stackexchange.com/questions/13866/why-is-the-ifthen-package-obsolete
- ↑ https://tex.stackexchange.com/a/232059/37907
Source: https://en.m.wikibooks.org/wiki/LaTeX/Macros
0 Response to "Illegal Parameter Number in Definition of currentlabelname to Be Read Again"
Post a Comment