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.

3
  • 5
    What don't you understand? In my opinion the FAQ is really clear: tex.ac.uk/cgi-bin/texfaq2html?label=linmacnames Commented Aug 10, 2012 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
    Commented Aug 10, 2012 at 16:53
  • NOTE: see the linked "similar question" for the solutions / workarounds. This question is for why it doesn't work.
    – user202729
    Commented Apr 10, 2022 at 17:41

3 Answers 3

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

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

3
  • 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. Commented Aug 10, 2012 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
    Commented Aug 10, 2012 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
    Commented Aug 10, 2012 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.

You must log in to answer this question.

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