5

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.

1
  • 1
    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

2 Answers 2

8

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 \(...\).

If you already have

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

4
  • Why is changing the catcode of _ fragile?
    – 1010011010
    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.
    – egreg
    Mar 28, 2015 at 15:31
  • @egreg Wouldn't that also lead to incompatibilities with LaTeX3?
    – 1010011010
    Mar 28, 2015 at 15:36
  • @1010011010 Not necessarily.
    – egreg
    Mar 28, 2015 at 15:36
2

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.

enter image description here

2
  • I would bet that the OP has \newcommand{\Z}{\mathbb{Z}}
    – egreg
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.