Questions tagged [macros]

{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.

95
votes
3answers
11k views

Defining starred versions of commands (* macro)

I would like to define a *'d version of a command, something like \newcommand{\foo}{blah} \newcommand{\foo*}{blahblah} If I try to do this, LaTeX complains that I'm trying to redefine \foo, so I ...
71
votes
9answers
7k views

Where do I find out how a command/environment is defined?

Say I want to redefine the itemize environment, or make a modified version of it. I want to know how it is currently defined. Where do I find this information? If it's some particular package command, ...
6
votes
5answers
450 views

Parse and execute the next word as a command?

I would like to have a macro, let's call it \csword, that reads the next word in the input (i.e. sequence of letters a-zA-Z), and then executes that word as a macro. For example \csword command \...
10
votes
3answers
868 views

Where do I find the documentation for writing \newcommand's ?

At what level of the TeX system heirarchy is \newcommand? Where do I find documentation for these kinds of macros (e.g. \renewcommand, \def, \parskip, etc.)? My objective is that I will be able to ...
7
votes
3answers
2k views

How to use \newcommand for \href?

I want to format Wikipedia entries in my document in a standard way: \href{http://en.wikipedia.org/wiki/Foo}{Foo} So I thought about using \newcommand*: \documentclass{article} \usepackage{ifthen} \...
3
votes
1answer
428 views

Verbatim inside def in eplain

How can I use \verbatim inside my own \def with eplain? The eplain reference suggests to change catcodes to accomplish this, but doesn't provide an example of this. Crarification: I would like to ...
4
votes
3answers
2k views

What are a good set of macros for writing beamer presentations?

I previously asked about tricks for reducing typing when writing beamer presentations. I thought it might be useful to write a few macros for common commands. E.g., something like this: \...
6
votes
1answer
687 views

How to display some text based on a condition inside a tabular environment?

I defined a variable \@afwezig that holds people that were not present at a certain meeting. \long\def\@afwezig{} \def\afwezig#1{\def\@afwezig{#1}} Now I want to make the header of the meeting ...
4
votes
2answers
914 views

Macro to multicol section content but not section title

\section{Introduction} I want to write a macro to that makes the above behave the same as: \usepackage{multicol} \begin{multicols}{2}[\section{Introduction}] In praise of code convolution avoidance:...
5
votes
1answer
444 views

Problem redefining an internal macro of a sty file

[I posted this question first on stackoverflow since I wasn't aware of the existence of this site] Hi, I am using a special usepackage to write my thesis in tex and I need to adjust one of the ...
8
votes
2answers
2k views

Please critique this “intermediate size” fraction macro.

I often write a lot of mathematics where I multiply various quantities/operators/etc. by fractions. The fractions are important for bookkeeping, but usually not the most important thing in the ...
2
votes
2answers
556 views

Plain TeX macros with \obeylines and no delimiters.

How can I define a macro, say \address, so that I can use it like this: \address 27/7 Montague Street Edinburgh and it would put the lines following it into another macro, say \addrinfo ? What I'm ...
16
votes
1answer
533 views

How do I ‘expand’ a control sequence `\let` to a character?

Consider the following: \let\1=X \edef\2{\1} \show\1 \show\2 The result is > \1=the letter X. > \2=macro: ->\1. Is it possible to ‘expand’ the meaning of \1 so that \2 contains just X? ...
29
votes
5answers
847 views

How to find a package name by a command name?

Often I'm trying to use a command in my document and it doesn't compile, because I haven't included the package I need for it. Of course, I know, that I can try to google it, but Google often finds ...
5
votes
2answers
760 views

Why do TeX list items use two backslashes?

In TeX packages one finds definitions of lists as: \def\l{\\\alpha\\\beta\\\gamma} The one backslash is understandable, it represent the active character for the macro i.e, \gamma, but what do the ...
3
votes
3answers
548 views

Preceding a command by an argument

\usepackage{amsmath,amssymb,amsopn}% read amsldoc.pdf for more details %\genfrac{left-delim}{right-delim}{thickness}{mathstyle}{numerator}{denominator} %\newcommand{\frac}[2]{\genfrac{}{}{}{}{#1}{#2}} ...
3
votes
2answers
427 views

Is \algorithmicand a standard command in the algorithmic package?

I have a tex file created by someone else which redefines commands of the algorithmic package to translate them to the Portuguese language. For example: \renewcommand{\algorithmicwhile}{\textbf{...
11
votes
4answers
798 views

What's the best/right way to test an argument to a macro?

This is essentially asking how to do a case switch in LaTeX. I want to define a macro so that if the argument passed is one thing, then something happens, and if not then something else happens. I ...
5
votes
1answer
134 views

Defining a command for linked and indexed section headings?

What is the (preferred way of) defining a command that places a heading (like \section, \chapter, etc.) creates a label for this heading that can be referenced places the heading in the TOC (I think ...
46
votes
3answers
11k views

Is there any way to generate custom errors/warnings in LaTeX?

I want something like \ifmmode\else\GenerateWarning{The command \backslash uzuka is supported only in math mode.}\fi, which is supposed to generate a warning "The command \uzuka is supported only in ...
55
votes
7answers
11k views

LaTeX macro expander

Is it possible to have LaTeX (or another program) to expand a the macros in a .tex file, and output the expanded LaTeX source? If no such program exists: are LaTeX macros processed simply as string ...
15
votes
5answers
2k views

Use column-separator & (ampersand) inside \newenvironment

I'd like to define a grammar environment which basically wraps a tabular environment. Inside the grammar environment I'd like to use an environment called production to insert rows. Example: \...
11
votes
4answers
3k views

Latex tag for making a comment appear or disappear in pdf?

Is it possible to tag sections of your .tex file so that you can make text appear or not? I have a long document and would like to be able to comment a certain section in each chapter, and produce ...
13
votes
5answers
1k views

Parse comma-separated list of command names with spaces

Problem This question is related to How can I specify a long list of math operators? – but with a twist: I want to allow spaces in in the comma-separated list, like so: \newacronyms{acm, ams, cpu, ...
7
votes
3answers
1k views

Defining commands of commands

If you want to include an image in your chapter title, what follows may be an option: \newcommand{\thechapterimage}{} \newcommand{\chapterimage}[1]{\renewcommand{\thechapterimage}{#1}} \def\...
6
votes
1answer
2k views

\newcommand and comma separated parameters

I would like to learn to manipulate comma separated lists in Latex. To give a concreet but artificial example, I would like to have this command: \swap{1,10} expand to this: {10,1} How can I ...
6
votes
1answer
1k views

How do I convert a character to a numeric value?

Now that I've gotten Will Robertson's excellent unicode-math package working to change the colour and style of letters in my mathematics (trust me, there is a reason) thanks to his answer to my ...
12
votes
1answer
1k views

A LaTeX 3 macro to typeset chemical compounds?

Out of curiosity, I wonder how would a macro coded in expl3 that does the basics of mhchem (as in section basics in the mhchem manual) looks like? I have found examples in TeX and LuaTeX in http://...
28
votes
7answers
3k views

How to split text into characters

I would like to define a function which splits an input text into its constituent letters, so I can process each letter individually (the background to this question is that I would like to put each ...
52
votes
9answers
161k views

typesetting column vector

I would like to define a command which typesets a column vector. For one vector I can have something like: \left( \begin{array}{c} a\\ b\\ \end{array} \right) I would like the command to produce ...
8
votes
6answers
532 views

Is it possible to define a command that can be used where literal <comma-separated-list> is normally required?

I suspect I'm asking for magic, but I'll go ahead anyway. Suppose I have a command that accepts an arbitrary-sized list of comma-separated terms. One that we're all familiar with is \usepackage ...
17
votes
3answers
4k views

Problem with redefining Tabular environment with the \renewenvironment ‎command‎

In a book I'm typesetting, I want the tabular environment to be in footnote size and the other body of the book to be in normalsize font. To do this, I've tried something like the following: ‎‎‎‎‎‎‎\...
45
votes
4answers
18k views

How to conditionally define a new command in LaTeX?

I'm trying to compile a LaTeX code with an older version of some package. Thus, I'm having a few issues due to undefined commands. How can I add conditional code so that I can define some work-around ...
14
votes
2answers
7k views

Performing basic arithmetic in custom commands

One thing annoying in sparklines is its verbosity and dependence on external arithmetics. For example, to depict a three bar graph of 16%, 53%, 31%, one has to write: \begin{sparkline}{4} \...
6
votes
2answers
481 views

Changing fontsize within soul underline

This question is related to How to create a newcommand for fontsize?. I would like to change the fontsize within \ul of the soul package. For example: \newcommand{\mycfs}[1]{\fontsize{#1pt}{1.2#1pt}\...
6
votes
3answers
4k views

How to create a newcommand for fontsize?

I am trying to create a command to modify fontsize with one parameter, it seems that the paramater is not expanding properly: \newcommand{\mycfs}[1]{\fontsize{#1pt}{1.2*#1pt}}
62
votes
7answers
17k views

How to define a command that takes more than 9 arguments

I have a mathematical transformation that takes 16 parameters (grouped into 3+8+5) and would like to make a latex command for it, so that I can easily change the notation for it if the need arises. ...
27
votes
9answers
1k views

When is it appropriate to use \outer?

\outer is a strange beast. Any control sequence declared \outer cannot be used as either the parameter text or replacement text of a macro, at least if the control sequence is declared \outer at the ...
7
votes
4answers
555 views

Custom acronyms via \def

I'd like to define acronyms via the \def command. I know that the acronym package exists, but that's too much typing (\ac{myacronym} vs. \myacronym). The problem that I have is that for \def\...
15
votes
3answers
7k views

\renewcommand{\cite} does not work

I'm using \cite a lot in my document, but now I discovered that I really need \citep instead (from natbib). No problem, I thought, just redefine the \cite command: \renewcommand{\cite}[1]{\citep{#1}} ...
41
votes
8answers
5k views

Why do people use unnecessary braces?

I often see people creating new macros where the macro token is surrounded by braces. For example, \newcommand{\foo}{foo} This is unnecessary and I find the extra braces make it harder to read. Why ...
11
votes
2answers
2k views

Undocumented useful commands in Tikz

\tikzstyle{...}=[...] This command seems to be extremely useful since it allows to re-use the same style in multiple TikZ pictures. Is is used in a few places in TikZ manual (pgfmanual.pdf), but why ...
2
votes
4answers
2k views

Defining a good tab-able macro for matrices.

I want to declare some macros in my preamble which allow me to create 2 x 2 matrices more quickly, and in particular, I want to be able to tab through the four entry positions. I feel like this is ...
2
votes
4answers
417 views

Macro definition: Ignoring leading “MR” and trailing garbage begun by non-numeric / Parsing the mrnumber returned by MathSciNet

I have a bibtex .bst file that issues something like: "\MR{" mrnumber "}" * * in order to produce things like: \MR{MR123456789 (99 \#9999)} or: \MR{MR123456789} inside the .bbl file. This is ...
7
votes
3answers
338 views

Avoid inputting a file multiple times

I have a revision string automatically saved to a file VERSION (it is the output of git describe and usually looks like v2.2-19-g0a51c27), and define a command \newcommand*\fileversion{\input{VERSION}...
10
votes
3answers
707 views

What are the right rites to \write right now?

(with apologies for the title) I've just entered the murky waters of trying to get one LaTeX file to write out something useful as it goes along. In brief, I want my lecture LaTeX file to be able to ...
7
votes
3answers
4k views

How to create custom Tikz nodes?

I don't want to change style only. I want to create node that takes 3 numbers and display them with appropriate decorations. For instance: \mynode{5,6,7} Should display node with "a=5", "b=6" and "...
24
votes
6answers
3k 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 ...
8
votes
1answer
5k views

Define command for bold list-item

I have a set of commands for producing various kinds of specific list-items, such as \newcommand{\staritem}{\item[$\star$]} % not really, but you get the gist All the ones I currently have can be ...
22
votes
2answers
3k views

Problem with defining shortcuts for TikZ matrices

For answering a question yesterday I wanted to define a shortcut for creating a matrix inside a tikzpicture. As a minimal example, consider \newcommand\mymatrix[1]{ \begin{tikzpicture} \...