2

I find myself doing a lot of things like this below in order to compile document for both pdf and tex4ht

\ifdefined\HCode
  \includegraphics[width=0.5\paperwidth]{foo.eps}
\else
  \includegraphics[width=0.5\paperwidth]{foo.pdf}
\fi

So I said to myself on a bright moment, why not write a command called \includegraphicsX and implement this logic inside it? So the code will look like this:

\includegraphicsX[width=0.5\paperwidth]{foo}

The problem is that \newcommand only likes to accept arguments using {}{}, so I had to write the \newcommand like this: (MWE)

\documentclass[11pt,notitlepage]{article}%
\usepackage{graphicx}
\newcommand{\includegraphicsX}[2]
{
\ifdefined\HCode
  \includegraphics[#1]{#2.eps}
\else
  \includegraphics[#1]{#2.pdf}
\fi
}
\begin{document}
\includegraphicsX{width=0.1\paperwidth}{1_pic}
\end{document}

And the above works. But I would prefer to call the command using

    \includegraphicsX[width=0.1\paperwidth]{1_pic}

and not as shown in the MWE which is

   \includegraphicsX{width=0.1\paperwidth}{1_pic}

The reason is that, if I change my mind later (and I change my mind allot) and do not want to use the macro anymore, I would then only need to remove the one letter 'X' from the call using the editor find/replace, instead of also changing the brackets around the first argument which is harder to change automatically.

Is there a trick to define a command in Latex which accepts [] as first argument and not as {} ?

Using Tl 2015

7

Use \newcommand\commandname[2][]{<code>} for standard \commandname[<optional arg>]{<required arg>} syntax.

\documentclass[11pt,notitlepage]{article}%
\usepackage{graphicx}
\newcommand{\includegraphicsX}[2][]
{%
\ifdefined\HCode
  \includegraphics[#1]{#2.eps}%
\else
  \includegraphics[#1]{#2.pdf}%
\fi
}
\begin{document}
\includegraphicsX[width=0.1\paperwidth]{1_pic}
\end{document}

My answer to a more general question about creating commands and environments may be of interest.

Note that you can probably just use

\newcommand\includegraphicsX[2][]{%
  \includegraphics[#1]{#2}}

which, as Manuel points out, makes \includegraphicsX[]{} equivalent to \includegraphics[]{} so you do not really need to create the new command for this use at all. If your actual definition is more complex, obviously you will still need it but can maybe use the above to simplify it somewhat.

graphicx can figure out appropriate extensions, depending on the engine in use etc, and it is generally better not to specify these explicitly.

  • 1
    maybe not that important for this special macro and its supposed usages but I'd remove the spurious spaces anyway. – clemens Jul 22 '15 at 19:44
  • @clemens Thanks. I copied it quickly earlier but you are quite right. I've tidied up a bit. – cfr Jul 22 '15 at 20:40
  • 1
    @cfr In your last sentence, may be you could just tell the OP to use \includegraphics directly :) – Manuel Jul 22 '15 at 20:46
  • @Manuel True. I thought maybe the command given here was simplified for purposes of creating the MWE. If it is really just what's posted, then that is indeed an excellent point.... Edited.... – cfr Jul 22 '15 at 20:53
  • Note that you can probably just use humm... but this does not really work for me? I need new command in order to do the ifdefined\...\else...\fi logic in it. I am using your first method in your answer, and it is working well as it is. thanks again. – Nasser Jul 23 '15 at 1:27
3

Here is the xparse style of defining a command with optional argument, here with default empty optional argument.

\documentclass[11pt,notitlepage]{article}%
\usepackage{xparse}
\usepackage{graphicx}
\NewDocumentCommand{\includegraphicsX}{O{}m}{%
  \ifdefined\HCode
  \includegraphics[#1]{#2.eps}
  \else%
  \includegraphics[#1]{#2.pdf}
  \fi%
}
\begin{document}
\includegraphicsX[width=0.1\paperwidth]{1_pic}
\end{document}

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.