4

I have the following code in my latex command .

\newcommand{\color}[1]{\lceil (#1+2)^2/2\rceil}

After that I have inserted the following command for insertion of code in LATEX documents .

\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true
  tabsize=3
}

But TEXMAKER compiler shows me error that command \color is already defined. Why does compiler show error for insertion of these code ? Can you suggest me an way to solve this issue ?

12
  • 3
    Please post a MWE, not just a fragment. But in this case, \color is already defined by the package color or xcolor which is either included by you or by another package you used. If you explicitly want the command \color as in your example, you must redefine it via \renewcommand, but I do not recommend this. Chose another command name instead, please
    – user31729
    Commented Jul 12, 2014 at 5:21
  • 2
    The short answer: Don't define \color, since it is used/defined by the color package. Strange though, your command \color doesn't even use anything related to colour... so why not define it as \mycommand?
    – Werner
    Commented Jul 12, 2014 at 5:23
  • 2
    The question is not directly connected to texmaker, since any other IDE or direct compilation on console would provide the same error message
    – user31729
    Commented Jul 12, 2014 at 5:27
  • 3
    @user54714: If the template defines \color, then add \let\color\relax immediately after loading it. Then you can go ahead and \usepackage{color} as you already do.
    – Werner
    Commented Jul 12, 2014 at 5:40
  • 2
    @user54714: For that, see Underscores in words (text).
    – Werner
    Commented Jul 12, 2014 at 5:53

1 Answer 1

3

It seems strange to define \color as a macro that doesn't have anything to do with setting a colour attribute. The best option here is to remove this definition by adding

\let\color\relax

before loading the color package. Letting a macro to \relax frees it up, almost like it has not existed before.

4
  • if the op is focused on writing about (say) quarks, (s)he may be wanting a command to specify quark colour ... and hence has become confused. so it's not all that strange. Commented Jul 12, 2014 at 8:23
  • Redefining \color will create zillions of problems when lstlisting is used, of course. I consider this answer very wrong.
    – egreg
    Commented Jul 12, 2014 at 9:50
  • 1
    @egreg: You understand that the problem is with the template and that my answer allows \color to be restored by the color package, right?
    – Werner
    Commented Jul 12, 2014 at 11:57
  • @Werner Probably it should be clearer what the order should be, but you're right.
    – egreg
    Commented Jul 12, 2014 at 13:00

You must log in to answer this question.

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