1

I'm trying to write an easier color command \c{} that is shorter and easier to use than \color{}. However, I do not want to overwrite an existing command, so I will use \providecommand{}[]{} to create the command.

My question is: How does command options and arguments work?

The command needs to have 1 mandatory argument, the color. Also, like the \color{} command, the argument needs to be a defined color.

I'm thinking something like:

\documentclass[a4paper]{article}
\usepackage{xcolor}
\definecolor{netflixred}{HTML}{E50914}
\definecolor{overleafgreen}{HTML}{138A07}

\providecommand{\c}[1]{\color{#1}}

\begin{document}
    {\c{netflixred}This text is in netflixred}, 
    {\c{overleafgreen} and this is in overleafgreen}
\end{document}

But how do I create the argument for the command? And how do I, in general, create optional and mandatory arguments for commands?

1
  • Yes, thats why I said I would use \providecommand to NOT overwrite an existing one.
    – Vebjorn
    Nov 6, 2019 at 18:01

1 Answer 1

2

The traditional TeX way to define a new command is with \def; this works in LaTeX, as well. Arguments are given numbers with #; e.g., #1#2:

\def\hello#1#2{Hello, #1!  I'm #2!}

However, \def will not warn you when you're overwriting existing commands. \newcommand is the LaTeX version, and it offers some additional benefits, including a refusal to overwrite a command that already exists.

\newcommand{\hello}[2]{Hello, #1!  I'm #2!}

If you want to include an optional argument, include another square-bracket bit; the second point in square brackets indicates that there's an optional argument, and gives its default value if it's not specifically included:

\newcommand{\hello}[2][Joe]{Hello, #2!  I'm #1!}

The optional argument will be #1, and subsequent arguments will be mandatory. So if you say \hello{Mike}, you'll get Hello, Mike! I'm Joe!; but if you run \hello[Jim]{Mike}, you'll get Hello, Mike! I'm Jim!.

So, for your color command, I would suggest the following:

\newcommand{\co}[1]{\color{#1}}

That said, I'm not sure how much easier you could want than \color{blue}. Is there some particular reason that this is too long or cumbersome for you? Five keystrokes is a pretty tiny overhead.

Also, do not redefine \c. That's an accent command, for the cedille, and it should really be left alone.

4
  • Thanks. Does it work with several optional arguments?
    – Vebjorn
    Nov 6, 2019 at 18:05
  • No, I'm afraid not; it only allows one optional argument. Multiple optional arguments requires some dark TeX magic that's almost assuredly overkill for your problem. Nov 6, 2019 at 18:06
  • \newcommand{\co}[1]{\color{#1}} could more easily be written \newcommand{\co}{\color} although I would use \textcolor here rather than \color. Nov 6, 2019 at 19:42
  • Good point. \textcolor is what to use if you've got a short piece you want colored. Nov 6, 2019 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .