30

I would like put picture into table, and my idea is define new command as below>

  • \newcommand{\EJ471}{\includegraphics[scale=0.150]{EJ_471.jpg}}

But compiler reports an error. If I use the command name for example \EJa, everything is all right. I've read similar questions as

But still I don't understand why it does not work.

JardaFait
  • 3,922
  • 5
    What don't you understand? In my opinion the FAQ is really clear: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=linmacnames – Marco Daniel Aug 10 '12 at 15:28
  • @Jafan It's not normal here to include 'thanks' or similar: voting is the best way to indicate that something is helpful. – Joseph Wright Aug 10 '12 at 16:53
  • NOTE: see the linked "similar question" for the solutions / workarounds. This question is for why it doesn't work. – user202729 Apr 10 '22 at 17:41

3 Answers3

41

A long time ago in a galaxy far, far away....

someone wrote this:

http://web.archive.org/web/20130727172127/http://www.elec.ryukoku.ac.jp/~fujii/pub/ftp/incoming/styles/kth.se/numdef.sty

David Carlisle
  • 757,742
40

By the rules of TeX syntax, the "name" of a macro that starts with a \ (backslash) character must either

  • consist of a single non-alphabetical character. Some examples: \, (insert thin space), \% ( the % character), \\ (insert line break), \[ (open display math), and \) (close inline math).

or

  • contain only uppercase and lowercase alphabetical characters: a-z and A-Z. No numerals, and no other characters belonging to non-letter categories either. (Well, there are certain ways of assigning "letter-category" status to non-letter characters, but that's a topic for a different discussion.)

Therefore, \EJ471 is not a valid macro name.

However, you could define a somewhat more general macro:

\newcommand{\EJ}[1]{\includegraphics[scale=0.150]{EJ_#1.jpg}}

and use it as in \EJ{471} to pass EJ_471.jpg to the \includegraphics command. If you needed to process further jpg files that start with EJ_ (and end in .jpg, of course), you could simply keep invoking this macro with the appropriate arguments.

Mico
  • 506,678
  • Well, there are certain ways of assigning "letter-category" status to non-letter characters, but that's a topic for a different discussion. -- I think this is the correct place ;-). The catcodes are the reasons for this. – Marco Daniel Aug 10 '12 at 17:24
  • 2
    @MarcoDaniel - I have a feeling that the OP isn't all that interested in learning about catcodes just so that he/she can make \EJ471 into a valid TeX macro name after all. However, I may be wrong. :-) Please feel free to provide a separate, longer answer. – Mico Aug 10 '12 at 17:40
  • 1
    @MarcoDaniel As Mico says, the question here seems to be focussed on one part of the story. Catcodes are 'advanced', and moreover we've already got questions about them. – Joseph Wright Aug 10 '12 at 18:14
0

There are a variety of methods given here.

Combining some of them I've found this to be the best for a similar situation I was in.

\newcommand{\EJ}[1]{\csname EJ#1\endcsname}
\expandafter\newcommand\csname EJ471\endcsname{\includegraphics[scale=0.150]{EJ_471.jpg}}
\expandafter\newcommand\csname EJ8table\endcsname{\begin{tabular}{c|c} E & J \end{tabular}}

\EJ{471} \EJ{8table}

This is in the same spirit as Mico's answer, but a little more versatile.

Aeryk
  • 143