I am trying to call ImageMagic to crop my pictures. I send command to system with \write18, and I need to use percent symbol into my command line. Can't use "\%", but \@percentchar works almost good. Almost because it ignore all the spaces after macro and within lines:

\immediate\write18{start cmd echo /k magick convert _SDC2664.jpg  -crop 50\@percentchar x100\@percentchar +repage _SDC2664.jpg}

I get an error

"convert: invalid argument for option '-crop': 50%x100%+repage @ error/convert.c/

So, I need a space, " ". Make "50%x100%+repage" into 50%x100% +repage

Any suggestions?


Use \@percentchar\space

(A good question is: why is \@percentchar only an internal command? The more-available \percentchar is defined in doc.dtx.)

| improve this answer | |

You can define your own command


and use

\immediate\write18{start cmd echo /k magick convert _SDC2664.jpg  -crop 50\wpercent x100\wpercent +repage _SDC2664.jpg}

If you don't plan to pass the command line as an argument to another command, you can do

\newcommand{\magick}{\begingroup\catcode`\%=12 \@magick}

\magick{start cmd echo /k magick convert _SDC2664.jpg  -crop 50% x100% +repage _SDC2664.jpg}

(your editor’s syntax coloring might be upset by this, as you see, add \magick to the list of verbatim-like commands).

If your conversions use exactly the same parameters, you can do it in an easier way:

\providecommand{\magick}{}% to check it's not defined
\gdef\magick#1{start cmd echo /k magick convert #1 -crop 50% x100% +repage #1}

and then simply call \magick{_SDC2664.jpg}

Is it possible without \gdef? Yes:

\begingroup\lccode`?=`\% \lowercase{\endgroup
  \newcommand{\magick}[1]{start cmd echo /k magick convert #1 -crop 50? x100? +repage #1}

which exploits the fact that there is no uppercase character in the “parameterized” command line. This won't affect uppercase in the argument at call time, because \lowercase is only executed at definition time.

| improve this answer | |
  • Hmmm... why I can't do like this: \newcommand{\magick}{\begingroup\catcode`\%=12 \@magick} \newcommand{\@magick}[1]{\immediate\write18{start cmd echo /k magick convert #1 -crop 50%x100% +repage #1}\endgroup} \magick{ _SDC2664.jpg} – Vladimir May 30 at 10:18
  • 1
    @Vladimir You can certainly do it, if all conversions have the same parameters. I added this to my answer. – egreg May 30 at 10:28

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.