I would like to know if renaming a command is possible in LaTeX.
For instance, could I change the \section
by \sec
?
I read that some people used something called "alias".
macros
I would like to know if renaming a command is possible in LaTeX.
For instance, could I change the \section
by \sec
?
I read that some people used something called "alias".
Here is a way to use the xparse
package to make the first two parameters optional:
Unless we are totally replacing the existing definition, command redefinitions are usually tweaks of existing commands. So, we still need access to the original functionality. Hence, command redefinitions are usually done in two stages: The first is to save the existing definition under a new name, and the second is to use this older definition when required.
First save the original definition of \multicolumn
as \OldMulticolumn
. You can do this with:
\let\OldMulticolumn\multicolumn
In this case \let
is fine, but
if you wish to redefine a macro that has optional parameters, you have to use \LetLtxMacro
from the letltxmacro
package . A detailed description of \LetLtxMacro
can be found at this question at closed square root symbol. So in this case it is not necessary to use \LetLtxMacro
, but is fine to do so.
Then, we redefine \multicolumn
as desired and use \OldMulticolumn
when we need to invoke the original multicolumn
functionality:
\DeclareExpandableDocumentCommand{\multicolumn}{O{2} O{c |} m}{%
\OldMulticolumn{#1}{#2}{#3}%
}%
From the link in the References section the command has to be expandable since it is used within a tabular
environment, so \DeclareExpandableDocumentCommand
works.
The next part {O{2} O{c |} m}
provides the specification of the three parameters.
The first O{2}
says that the first parameter, #1
, is an optional parameter (specified in square brackets) and if it not provided it defaults to 2
. In this particular case this is the number of columns that the \multicolumn
is to span.
The next O{c |}
says that the second parameter, #2
, is also optional and if not provided it defaults to c |
. This is the alignment specification that would be the second parameter of the original \multicolumn
.
The last parameter is of type m
which means that this is a mandatory parameter that needs to be provided with the usual {}
.
So, based in the arg spec of ...{\MultiColumn}{O{2} O{c |} m}
(using a different name here to hopefully avoid confustion with the existing \multicolumn
) the following three would equivalent:
\\MultiColumn{xyz}%
\\MultiColumn[2]{xyz}%
\\MultiColumn[2][c |]{xyz}%
all three have the parameters #1=2
, #2=c |
, #3=xyz
.
For more details please refer to the xparse
package documentation.
tabular
environment requires expandable content. See Misplaced \omit. \multispan with \newcommand for more details.\documentclass{article}
\usepackage{xparse}
\usepackage{letltxmacro}
\LetLtxMacro{\OldMulticolumn}{\multicolumn}
\DeclareExpandableDocumentCommand{\multicolumn}{O{2} O{c |} m}{%
\OldMulticolumn{#1}{#2}{#3}%
}%
\begin{document}
\begin{tabular}{|l | l |l |}\hline
a & \multicolumn{some long centered content}\\\hline
b & c & d\\\hline
a & \multicolumn[2][l|]{left content}\\ \hline
\end{tabular}
\end{document}
The above is the normal usage of \multicolumn
. Note that in order to specify the alignment, the number of columns needs to be specified. One could certainly enhance the macro to test for an empty parameter and replace it with the default value -- that is using it as \multicolumn}[][r |]{x}
would be equivalent to \multicolumn}[2][r |]{x}
.
However, in my cases the number of columns to span in a table is usually fixed so I prefer to swap the order of the options and so that I can just provide the alignment. Now using \multicolumn[r |]{right content}
I get r
ight aligned content spanning the 2
columns:
\documentclass{article}
\usepackage{xparse}
\usepackage{letltxmacro}
\LetLtxMacro{\OldMulticolumn}{\multicolumn}
\DeclareExpandableDocumentCommand{\multicolumn}{O{c |} O{2} m}{%
\OldMulticolumn{#2}{#1}{#3}%
}%
\begin{document}
\begin{tabular}{|l | l |l |}\hline
a & \multicolumn{some long centered content}\\\hline
b & c & d\\\hline
a & \multicolumn[r |]{right content}\\ \hline
\end{tabular}
\end{document}
Rather than mess with the interpretation of {
it is probably better to define a command that takes a comma separated list of arguments and encloses each in braces. Some variants below depending on whether you want a brace around the outer list, and whether you want a comma separator. (Beware the space after #1
is needed otherwise the case with as single argument brace group {a,b,c}
does not work as TeX drops the brace.
\documentclass{article}
\makeatletter
\def\sets#1{\@for\tmp:=#1 \do{\{\tmp\}}}
\def\setsb#1{\{\@for\tmp:=#1 \do{\{\tmp\}}\}}
\def\setsc#1{\def\sep{\def\sep{,}}\@for\tmp:=#1 \do{\sep\{\tmp\}}}
\def\setsd#1{\{\def\sep{\def\sep{,}}\@for\tmp:=#1 \do{\sep\{\tmp\}}\}}
\makeatother
\begin{document}
$\sets{a,b,c}$ $\sets{a,{b,c}}$ $\sets{{a,b,c}}$
$\setsb{a,b,c}$ $\setsb{a,{b,c}}$ $\setsb{{a,b,c}}$
$\setsc{a,b,c}$ $\setsc{a,{b,c}}$ $\setsc{{a,b,c}}$
$\setsd{a,b,c}$ $\setsd{a,{b,c}}$ $\setsd{{a,b,c}}$
\end{document}
Best Answer
There are a number of ways, depending on what you're after:
\let<csnameA><csnameB>
makes a copy of the definition of<csnameB>
and places it into<csnameA>
(like a regular copy-and-paste). It has the advantage that you can now redefine<csnameB>
without affecting the copy you just made (<csnameA>
).\newcommand{<csnameA>}{<csnameB>}
merely points<csnameA>
to<csnameB>
. To that end, updates to<csnameB>
will still be reflected in<csnameA>
.Related questions:
\LetLtxMacro
?\let\foo\bar
vs\let\foo=\bar