3

I'm trying to get this command into my LaTeX document but it causes the compiler to fail, saying "Missing \begin{document}":

\newcommand{\C*}{\mathbb{C} \setminus \lbrace 0 \rbrace}

I have also tried using \C0 instead of \C* thinking the asterisk might cause an issue, but to no change in the error message. The error message also says I can't use \mathbb{.} outside math mode, which I am not trying to do. Using \newcommand{\CC}{\mathbb{C}} is working fine in my document, I don't understand the difference to be honest and thus I can't figure out how to fix the issue.

  • 3
    command names have to be letters or a single non letter you can not have a command \C* or \C0 – David Carlisle May 12 '18 at 14:28
  • 2
    Don't worry about the math error, the first is important. That one got the typesetting engine running and you are now tryng to use \mathbb outside of math mode. Fixing the first error will not throw LaTeX off the wagon. – Johannes_B May 12 '18 at 14:30
7

Command names have to be letters or a single non letter you can not have a command \C* or \C0

After the first couple of expansions your input is more or less the same as this, which gives the same error.

\documentclass{article}


\newcommand{\C}{}{*}{\mathbb{C} \setminus \lbrace 0 \rbrace}

\begin{document}

\end{document}

So \C gets defined

 \newcommand{\C}{}

then it starts a paragraph of text with the symbol * which gives the missing document error as you can not typeset text before \begin{document}

{*}

Then if you scroll past that it tries to typeset

{\mathbb{C} \setminus \lbrace 0 \rbrace}

which gives an error because it is not in math mode.

| improve this answer | |
  • 1
    Ah I see my mistake now. I‘m pretty new to using LaTeX and didn‘t know commands can only consist of letters. Thank you! – Strupp1 May 12 '18 at 15:45
4

You already know why it doesn't work. If you really want to have \C to do \mathbb{C} and \C* to do \mathbb{C}\setminus\{0\}, you can do it easily:

\usepackage{xparse}

\NewDocumentCommand{\C}{s}{%
  \mathbb{C}\IfBooleanT{#1}{\setminus\{0\}}%
}

With \IfBooleanT{#1} we check for the presence of *.

Full example

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}

\NewDocumentCommand{\C}{s}{%
  \mathbb{C}\IfBooleanT{#1}{\setminus\{0\}}%
}

\begin{document}

The set of complex numbers is $\C$. If we remove~$0$
we get $\C*$.

\end{document}

enter image description here

If you prefer \C0, here it is:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}

\NewDocumentCommand{\C}{t{0}}{%
  \mathbb{C}\IfBooleanT{#1}{\setminus\{0\}}%
}

\begin{document}

The set of complex numbers is $\C$. If we remove~$0$
we get $\C0$.

\end{document}

The output is the same.

| improve this answer | |
0

can't figure out how to fix the issue

If by "fix" you mean define a command using one "other" character, like the * or 0, you could use \@ifnextchar

\documentclass[a4paper, twocolumn]{article}
\begin{document}
  \makeatletter
    % What to do if there is a *
    % Need an argument to "eat" the *.
    % (so in the below, the argument #1
    % is the asterisk)
    \newcommand\CStar[2]{GotAStar and #2}
    % What to do otherwise
    \def\CNoStar{\relax}
    % Define \C and delegat what should happen
    % if it's followed by a *
    \def\C{\@ifnextchar*\CStar\CNoStar}
  \makeatother
  \C*{ARG}
\end{document}

The answers following question explains what happens here pretty good: Understanding \@ifnextchar

| improve this answer | |
  • You could also use \@ifstar, which lets you define \CStar, more naturally, as a command with one argument. – GuM May 12 '18 at 15:42
  • Sure, but wouldn’t work for 0, right? – Andreas Storvik Strauman May 12 '18 at 15:55
  • Also, maybe you could do \catcode `*11 ? – Andreas Storvik Strauman May 12 '18 at 19:14
  • Of course, \@ifstar wouldn’t work with other characters than the star—I hadn’t noticed that part of the question. In any case see @egreg’s answer for the most elegant, LaTeX3-flavored solution. As for setting \catcode`*=11, since \@ifnextchar uses an \ifx test, I would expect this—among other things—to break all uses of the \@ifstar macro, which is already defined in the format with a “\catcode 12* character. – GuM May 12 '18 at 23:02
  • Agree with the elegance of @egreg's answer! – Andreas Storvik Strauman May 13 '18 at 7:17

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.