{macros} is for questions specifically concerning TeX's macro processor. Note that, because TeX uses macros ubiquitously, most questions about code that uses macros are not looking for information about how macros work, and so are not appropriate for this tag.

learn more… | top users | synonyms (4)

18
votes
1answer
589 views

Why use \DeclareTextFontCommand vs. just \newcommand?

Why should I use \DeclareTextFontCommand vs. just \newcommand? In the answer to my previous question, @egreg said: With \DeclareTextFontCommand you get a robust command that will remain ...
17
votes
3answers
836 views

Conditionally Remove Brackets

I'm trying to create a command with an optional argument using \newcommand{\Af}[1]{{\mathbf{A}_{(#1)}} where the argument is a number. However, I'd like that if no argument is given the bracket ...
17
votes
4answers
700 views

How to define a command that bolds the first letter of each word in the input argument?

I want to define a command that takes a sentence, a word or several words as input and bolds the first letter of each word. I need this mostly for abbreviations and so far I have done it manually in ...
17
votes
4answers
2k views

No space present after ensuremath

When I insert a short maths section in a sentence, it renders fine with a space each side as expected: ...returned $\mathcal{P}$ from... If I define a command to do the same thing, it also renders ...
17
votes
3answers
502 views

What is the exact purpose of \ftype@<TYPE>?

I currently working on some float related code an saw that the standard float environments figure and table define (beside others) the macros \def\ftype@figure{1} and \def\ftype@table{2}. The LaTeX ...
17
votes
3answers
747 views

What does \\* do?

I have some code, and when I use \\*, it causes some things to become unaligned, but when I use \\, the alignment is correct. What's the difference?
17
votes
3answers
2k views

Conditionals in \newcommand

I find myself using the \newcommand macro a lot and apart from having the occasional difficulty with properly defining the optional parameter the macro works well. There is one serious limitation ...
17
votes
3answers
2k views

Doing maths with distance values in LaTeX source code

I have a sty file with the command \newcommand{\foo}[1]{\noindent\kern#1} but I'd actually like to have something like \newcommand{\foo}[1]{\noindent\kern{#1 *0.5}} meaning that I'd like to have ...
17
votes
5answers
909 views

How to apply a macro to each column of a table

I want to apply a macro to all the cells of a column with the cell content as the input to the macro. Is this possible? For example if the cell contents have the word TEST, the macro should take TEST ...
17
votes
4answers
554 views

Extract the numerical and non-numerical portion from text

I would like to extract the leading number and subsequent text from a string. I have an idea of using the xstring package to gobble characters from the right until I end up with a number or an empty ...
17
votes
4answers
336 views

Is it possible to add an “or” condition to a \def?

Is it possible to have something like \def\fiveorsix{5,6 OR 6,5} So that the following works: \documentclass{standalone} \def\fiveorsix{5,6 OR 6,5} \makeatletter \newcommand{\test}[1][]{ ...
17
votes
2answers
320 views

Why do some commands use []s, some use {}s, and some use both?

I find it confusing to remember which commands take {} arguments, and which ones take [] arguments. Is there a rule-of-thumb or semantic difference between these two? If so, what? (Because they seem ...
17
votes
2answers
3k views

How do I use \show?

Q: How do I use \show to determine macro definitions?. I've followed advice here: The definitions of LaTeX commands and here: \show with fewer lines? and am still struggling. I've naively tried ...
17
votes
2answers
372 views

Why does this simple \ifx test fail?

This should be easy, but I don't understand why the test below fails... I expected Hurrah! to be printed when the \currentchar count reaches 90 (i.e. the character code of Z). There must be some ...
17
votes
5answers
1k views

Are there purely expandable variants of \MakeUppercase?

\MakeUppercase and \uppercase use TeX's uccode, and they are not purely expandable. Say, \edef\temp{\MakeUppercase{abc}} will fail. Sometimes purely expandable macros are very useful. They are ...
17
votes
2answers
223 views

Why is unskip not working here?

I'm trying to define a macro to be used as follows: \usepackage[normalem]{ulem} \newcommand{\thesis}[1]{\uline{\ignorespaces #1\unskip}} % underlining required by school - I know, I know % later... ...
17
votes
1answer
441 views

Newcommand: What cannot be done?

It is not the first time I tried to create a new command unsuccessfully. Some things I found out did not work (getting "undefined control sequence" or other errors). Use a quote-endquote argument. ...
17
votes
2answers
242 views

Can I turn a collection of optional parameters into a macro?

I currently have several environments from mdframed that have a large number of options that are shared between them. The situation looks like \newmdtheoremenv[options0,options1]{env1}{Environment 1} ...
17
votes
1answer
268 views

Why doesn't \makeatletter work inside \newcommand?

After hours of banging my brain against my skull, I narrowed down an issue I'm having to the fact that \makeatletter doesn't work inside \newcommand. Commands that I call after \makeatletter are ...
17
votes
2answers
535 views

Selecting random elements from a comma separated list

I would like to have a command (either user-defined, or from a package, or whatever) that has the basic form: \selectNrandom{N}{list, of, comma, separated, elements}{code to execute} which will ...
17
votes
1answer
798 views

Abbreviations and full stop--how to avoid double dots?

I have trouble with macros expanding to an abbreviated expression. Say I have \WHP which expands to "with high prob." (artificial example). I want to avoid the dot after the abbreviation colliding ...
17
votes
1answer
458 views

How do I make a macro require math mode?

I have defined a macro that I would like to be used exclusively in math mode. In order to enforce this, I would like the macro to throw an error message whenever it is used outside of math mode. What ...
17
votes
3answers
2k views

How do magic \dots work in amsmath?

The amsmath package redefines the \dots command so that it "guesses" the correct height of the dots with respect to its context. For example $x, \dots, y$ will produce three lowered dots, aligned ...
17
votes
1answer
2k views

Define same command with different amount of parameters

I want to define two versions of the command \Set, depending if I provide one or two parameters. I.e. like this: \newcommand{\Set}[1]{\bigl\{ #1 \bigr\}} \newcommand{\Set}[2]{\bigl\{ #1 \bigm| #2 ...
17
votes
2answers
8k views

Block comments with *% and %*

I know the package comment which allows to use : \begin{comment} Lines to comment \end{comment} This is not very friendly to use. I would like to have the following possibility: *% Lines to ...
17
votes
1answer
767 views

How to write overlay-specification–aware macros that work w/o the beamer class

The LaTeX beamer class provides with \newcommand<> and \newenvironment<> an interface to define own commands that can deal with beamer's overlay specifications. Internally, commands ...
16
votes
3answers
708 views

Short version of emph?

Is there something like a short version of \emph? What I want is something comparable to \lstShortInline or what the shortvrb provides, just not for the \verb command but for the \emph command ...
16
votes
4answers
907 views

How to draw the following box using TikZ?

How to draw the following boxes using TikZ? EDIT (22.09.)
16
votes
6answers
517 views

A new environment to decide dynamically which commands to run?

I am having some user defined commands defined and used in my latex document. I need a way to check whether any of the commands is used. I want to make decisions based on the argument used in the ...
16
votes
3answers
821 views

Shortcuts for code in latex

I am tired of writing \frac{\partial something}{\partial something_else}. Can I automate it by defining some sort of shortcut to it, so that when I write \p(x)(y) it will transform to \frac{\partial ...
16
votes
4answers
591 views

Do starred commands eat up space after them?

Do starred commands eat up spaces after them, like unstarred ones do? That is, is \somecommand* nextword equivalent to \somecommand*nextword? And: Does it depend on the particular way a starred ...
16
votes
3answers
2k views

What is the deal about these semantic vs syntactic commands?

I keep hearing people on this site talking about "semantic" vs "syntactic" commands and that the "semantic" ones are somehow better. I don't really get what they are talking about, could you explain ...
16
votes
3answers
701 views

Custom LaTeX logo

I'm trying to get this "logo" At the moment I use the following command \mbox{% M\hspace{-0.25ex}% \raisebox{0.18ex}{\tiny A}\hspace{-0.65ex}% T\hspace{-0.65ex}% \raisebox{0.18ex}{\tiny ...
16
votes
3answers
784 views

Using command to compute sum?

I must admit this sounds like an easy question. I'm creating a grading model for a homework assignment, and I'm adding the maximum amount of points to each question. Is there a way to have LaTeX ...
16
votes
3answers
266 views

Defining Extensible macros

Question: For a macro that has only one parameter, are there any issues with defining it as follows: \NewDocumentCommand{\MyMacro}{% s% #1 = starred variant (*** unused as of yet ...
16
votes
3answers
350 views

newcommand does not work with \fi

I tried the command \newcommand{\fistudenti}{\fi} but it seems to work properly only in some cases. Here below there are two insances when it works and when it does not: WORKS: ...
16
votes
3answers
547 views

Is it wrong to use \frac1{}?

When typing math in (La)TeX, I often omit the braces surrounding the first argument to the \frac macro, at least when it's 1, writing things like \frac1{1+x^2}. I initially came by that use while ...
16
votes
3answers
1k views

Why does LaTeX make a distinction between commands and environments?

Why does LaTeX make a distinction between commands and environments? Or also: why do we need environments? I ask this because I am defining a number of custom commands (like \question{} and ...
16
votes
2answers
2k views

Expanding arguments before macro call

I've been banging my head against this for days. I have a macro which takes a macro name and its arguments. It then re-arranges the arguments a bit and then calls the passed macro with the re-arranged ...
16
votes
4answers
891 views

Understanding a \@for loop

I'm having trouble with a \@for loop. I've got a macro that takes a comma delimited list and puts each element into a row of an array. When I do this using a \@for loop, I get an extra row that I ...
16
votes
3answers
1k views

Defining commands that are scoped to a particular environment

Is it possible to define commands in a separate namespace so that they only work within a particular environment? For example, can I create a package named foo that defines a foo environment and a ...
16
votes
2answers
462 views

What does the command \@dblarg in a class?

In the definition of a command appears the declaration of \@dblarg, as follows: \def\title{\@dblarg\CJ@title}. What exactly does \@dblarg do?
16
votes
4answers
1k views

Is there a test for the different styles inside maths mode?

Is there a test for the different styles within maths mode? What I really want is to be able to save the current style to reinvoke it later on, but a \if along the lines of \ifmmode would work just ...
16
votes
2answers
270 views

“Namethatchar” macro

I am searching for a macro/package that would give the name of characters. Here are a few examples of a possible behaviour: \namethatchar{A} → A \namethatchar{À} → Agrave \namethatchar{a} → a ...
16
votes
3answers
2k views

Detect beginning of a sentence in a macro for capitalization

Is it possible to make macros give different results at the beginning of a new sentence? Suppose that I want the macro "\secname" to write "Section" at the beginning of a new sentence, and "section" ...
16
votes
3answers
376 views

Print 'system information' in LaTeX doc

I've used TeX quite a bit before but am getting back into it at the moment. I want to print a document details page with info such as: Compiled on: Compiled by: OS version: LaTeX version: ...
16
votes
2answers
453 views

Duplicating Environments

When I want to globally modify a command in my document, I will often use a duplicated version of the original in the stencil, with the help of the \let macro. For example, if I wanted to do ...
16
votes
1answer
927 views

What does \marks do?

Recently I had cause to define a macro called \marks. If I try something like \newcommand{\marks}{fred} I get the error ! LaTeX Error: Command \marks already defined. In LaTeX, if I do ...
16
votes
2answers
492 views

Writing musical pitches

Is there a package or macro that allows me to easily typeset musical pitches, like the following: ? Indeed I would like to have subscripts for the octaves. Let's say $\mathrm{E}\flat_3$ How ...
16
votes
2answers
662 views

Macros in LaTeX and in programming languages

There is a fairly widespread consensus that macros are a bad idea in most programming languages. (For example, cf. http://scienceblogs.com/goodmath/2007/12/17/macros-why-theyre-evil/ .) That said, I ...