# macro help with $\mathbb{Z}/p^{a}\mathbb{Z}$

I am using \mathbb{Z}/p^{a}\mathbb{Z} many times in my work with variable a, I was thinking can I define a macro like \Z_p^{a} which will give the above result. I tried

\newcommand{\Z_p}[1]{\ensuremath{\mathbb{Z}/p^{#1}\mathbb{Z}}}


but LaTeX did not like it.

• Names of TeX macros defined via \def or \newcommand must consist of either one or more "letter"-class characters (A-Z and a-z) or exactly one non-letter character. The macro name Z_p, which contains two letter-class characters and one nonletter-class character -- is not valid under TeX's syntax rules.
– Mico
Mar 28, 2015 at 14:58

I'd go with

\newcommand{\Zp}[1]{\mathbb{Z}/p^{#1}\mathbb{Z}}


and $\Zp{a}$ will do what you want. It's not possible to have _ in the name of a macro (unless playing nasty and fragile tricks).

Maybe I'd do

\newcommand{\Zp}[2][p]{\mathbb{Z}/#1^{#2}\mathbb{Z}}


so, for the case where you want to use a different prime you can type

\Zp[q]{a}


Why not \ensuremath? Because it serves little purpose; I find it much better to segregate math in the proper way, that is, between \$ symbols or inside $$...$$.

\newcommand{\Z}{\mathbb{Z}}


it's better doing

\newcommand{\Zp}[2][p]{\Z/#1^{#2}\Z}


so a change of formatting for the integers will automatically produce the same change in \Zp. Better yet, I usually advice to do something like

\newcommand{\numberset}[1]{\mathbb{#1}}
\newcommand{\Z}{\numberset{Z}}
\newcommand{\Q}{\numberset{Q}}
% ... other possible number sets ...

\newcommand{\Zp}[2][p]{\Z/#1^{#2}\Z}


so uniformity is ensured for all alike symbols.

• Why is changing the catcode of _ fragile? Mar 28, 2015 at 15:29
• @1010011010 Would you like being also able to use subscripts in math? Yes, it can be done, I know, but I classify this as a nasty and fragile trick, as some packages may expect _ has its normal category code. Mar 28, 2015 at 15:31
• @egreg Wouldn't that also lead to incompatibilities with LaTeX3? Mar 28, 2015 at 15:36
• @1010011010 Not necessarily. Mar 28, 2015 at 15:36

With Plain TeX syntax you can do it:

\documentclass{article}
\usepackage{amsfonts}

\def\Z_p#1{\ensuremath{\mathbb{Z}/p^{#1}\mathbb{Z}}}

\begin{document}

\Z_p {137}% or Z_p{137}, it's all up to you

\end{document}


Notice that this overwrites a previous command \Z if it exists, but by default none exist. To be extra sure with other packages loaded, insert

\newcommand\Z{}


before the \def to give a chance to LaTeX to protest.

• I would bet that the OP has \newcommand{\Z}{\mathbb{Z}} Mar 28, 2015 at 16:41
• @egreg perhaps but why didn't he use \Z in his/her ensuremath then? and I recommend \ZZ...
– user4686
Mar 28, 2015 at 16:42