Hot answers tagged macros
20
There are of course some time considerations in looking for the arguments that are not there but they may not be measurable on modern machines.
However I wouldn't define a command with a star form, two optional arguments and a mandatory argument. If you need that many you will probably find yourself needing more so the plan of using #4 and not changing it ...
8
Perhaps what you are looking for is
\def\display#1{\texttt{\expandafter\strip@prefix\meaning#1}}
which gives the verbatim-ish rendering of the first level expansion of #1 but similarly to your example it doesn't really make sense to pas in more than a single token as #1.
Any later tokens in either your example or this one are just typeset as normal.
But ...
7
With e-TeX available, the \detokenize primtive does what you want: it turns all of the material into catcode-12 tokens, apart from spaces which are catcode 10. It also inserts spaces after control words. \detokenize has toks-like syntax, so can be used in the form
\def\display#1{\detokenize\expandafter{#1}}
The \detokenize primitive is expandable, so you ...
7
Control sequences are of two types:
control symbols, that is, backslash and one non category code 11 character;
control words, that is, backslash and any sequence of category code 11 characters.
The main problem is in deciding when a control word ends; the rules of TeX tell that a control word ends when the first non category code 11 character is found.
...
6
Two (or more) optional arguments have an obvious problem: you can't specify the second argument without specifying also the first one.
As an example, consider \makebox: you can do \makebox[3em]{text} or \makebox[3em][l]{text} and the second optional argument doesn't make sense if the width is not specified. A second example is \textcite of biblatex that has ...
6
As an addition to David Carlisle's answer, I want to add a better way to shift the argument from #1 to #4 with dummy arguments #1, #2, #3. Unless you already know what syntax you will want for your final version, you should put arguments #1, #2 and #3 that are unlikely to appear
\NewDocumentCommand{\MyMacro}{t.t.t.m}{\emph{#4}}
Here I picked an optional ...
6
One quite important difference, not mentioned in the discussion so far, is:
\newcommand\foo{...} corresponds to \long\def\foo{...}
\newcommand*\foo{...} corresponds to \def\foo{...}
What does \long mean? Well, if \myCommand is defined using \long, then its arguments can contain paragraph breaks, like this:
\myCommand{Lorem ipsum
dolor sit
amet.}
If ...
6
This is a place where \LetLtxMacro should not be used.
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{letltxmacro}
\newcommand*{\FormatColor}{}%
\NewDocumentCommand{\FormatText}{s m}{%
\IfBooleanTF{#1}{%
\def\FormatColor{red}%
}{%
\def\FormatColor{blue}%
}%
\textcolor{\FormatColor}{#2}%
}%
...
6
The 'big picture' aim here is to make sure that no ligatures are applied: for example, -- is converted to an en-dash in 'normal' circumstances as it's a ligature. The way this is done is to make the potential ligature characters 'safe' inside the verbatim environment by inserting a kern between them.
The detail you ask about is as follows. The macro ...
6
Here you go...
\documentclass{article}
\newcommand*\foo[2]{\frac{\mathrm{d}^{#2}#1}{\left(2\pi\right)^{#2}}}
\begin{document}
$\foo{q}{4}$
\end{document}
Notes
The [2] means that \foo takes two parameters.
These parameters are referred to as #1 and #2 inside the definition of \foo.
The exponent is written with braces around it -- that is, blah^{#2} ...
4
Taking some hints from Package xparse \SplitList last token, you can define a list processor that performs your request iteratively:
\documentclass{article}
\usepackage{graphicx,xparse}% http://ctan.org/pkg/{graphicx,xparse}
\newcounter{itemcntr}
\NewDocumentCommand\createanswerbox{O{,\,} >{\SplitList{,}}m}
{%
\setcounter{itemcntr}{0}% Start at 1.
...
4
For simple and complex diagrams, I'd recommend tikz-cd. Let's see an easy triangular diagram.
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
\begin{tikzcd}
A \arrow{r}{f} \arrow[swap]{dr}{g\circ f} & B \arrow{d}{g} \\
& C
\end{tikzcd}
\]
\end{document}
An arrow takes as argument the "steps" where it has to go: r stands for ...
3
I'd say
\let\oldquote\quote
\let\oldendquote\endquote
%% the following to please environ
\let\quote\relax
\let\endquote\relax
\NewEnviron{quote}{%
\xdef\dolatercoverandabstract{%
\noexpand\coverandabstract{\unexpanded\expandafter{\BODY}}%
\unexpanded{\let\quote\oldquote\let\endquote\oldendquote}%
}\aftergroup\dolatercoverandabstract}
Without ...
2
If you stick with environ you can lift the code out of the group with \aftergroup
\documentclass{article}
\def\coverandabstract#1{\twocolumn[#1]}
\usepackage{environ}
\let\oldquote=\quote
\let\endoldquote=\endquote
\let\quote\relax
\let\endquote\relax
\NewEnviron{quote}%
{\global\let\tmp\BODY\aftergroup\docoverandabstract}
...
2
Is the following simply what you are looking for?
\documentclass{article}
\newcommand*\horse[3]{\frac{\mathrm{d}^{#1}#2}{\left(2\pi\right)^{#3}}}
\begin{document}
\[
\horse{3}{p}{a} \quad
\horse{4}{q}{b}
\]
\end{document}
(This is after an updated answer.)
1
I might have something in the right direction. I had to figure this out to write a best practices document where I had to very clearly identify the best practices, be able to reference them, and list them.
The solution I have uses memoir, and defines a new environment which is numbered. I am pretty sure I got some of the code from the memoir manual and ...
1
Package environ to capture the contents of an environment. LaTeX3 code (package expl3) to split it into lines, cells, and more general programming tools. Rather than making an environment with 4 parameters as I did, you can hard-code some of them in the definition if it is more practical.
\documentclass{article}
\usepackage{environ}
\usepackage{expl3}
...
1
I suggest you to add the following lines to your class files:
\RequirePackage{catchfile}
\newcommand{\getaddressfrom}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\newcommand{\defineaddress}[1]{\def\thetoaddress{#1}}
and change the part where you start the letter with
\AtBeginDocument{%
\begingroup\def\tempa{\endgroup\begin{letter}}
...
Only top voted, non community-wiki answers of a minimum length are eligible

