9

I can use \define to define a new command, but how do I overwrite an existing one?

My use case is rewriting all {\externalfigure[cow]} to \placefigure[right]{My Caption}{\externalfigure[cow]} to float them.

  • The garden likens it to \def - I've never used ConTeXt, but \def will always overwrite a definition in Plain TeX. Have you tried simply \defineing it again? – Sean Allred Jul 22 '13 at 13:39
  • 1
    Do not redefine a core macro like \externalfigure. – Aditya Jul 22 '13 at 14:18
  • the reason I want redefine a core macro is that I'm using Pandoc which emits \externalfigures in the body text. I can only change the surrounding code in the template. – mb21 Jul 22 '13 at 14:20
  • or can I float specific figures with \defineexternalfigure somehow? – mb21 Jul 22 '13 at 14:50
  • pandoc generates floating figures by default. (You need to leave a newline after the figure specification). – Aditya Jul 22 '13 at 17:05
7

(Re)defining commands

\define does overwrite an existing command:

\define\foo{Foo}
\define\foo{Bar}
\starttext
  \foo  %% Bar
\stoptext

This prints a message to the log file that the command is already defined, but the command is being redefined. If you do not want to overwrite an existing command, you can use \unique:

\def\foo{Foo}
\unique\def\foo{Bar}
\starttext
  \foo  %% Foo
\stoptext

This prints a message to the log file as well but retains the old definition.

command '\foo' is already defined

Your use case

I would not tinker with TeX macros or trying to redefine core macros. Use your editor's search-replace feature or write a regular expression to prepend \placefigure to the \externalfigure calls.

If you really want a dirty TeX hack, here's my shot:

\let\externalfigureOrig\externalfigure

\def\externalfigure[#1]%%
  {\placefigure[right]{}{\externalfigureOrig[#1]}}
  • thanks, with #1, #2 I can access the contents in the curly braces of the command call. But what about the options in the various brackets []? btw, commented about my use case above. – mb21 Jul 22 '13 at 14:33
  • I don't quite get the question. You prepend all the options required for \placefigure and you leave all options for \externalfigure. And maybe append the closing brace. – Marco Jul 22 '13 at 15:11
  • \define[1]\externalfigure{\placefigure[right]{}{\externalfigure[#1]}} obviously doesn't work when calling it with \externalfigure[cow.jpg]. I always get ! TeX capacity exceeded, sorry [save size=50000] – mb21 Jul 22 '13 at 15:57
  • thanks! exactly what I was looking for :) Maybe one day pandoc will allow a more elegant solution. – mb21 Jul 22 '13 at 16:21
2

The below is not the answer to the question in the title, but it might help you nonetheless. It uses, I think, pandoc's property of directly passing through what doesn't match the input format's syntax.

Because your pipeline is something like this:

[ reStructuredText ]     pandoc
[ or Markdown      ] --------------> [ context ]
[ or ...           ]

you might be interested in the pandoc+preprocessor solution on Aditya's blog.

In that post, he describes using gpp, the generic pre-processor, to get output-format-specific code into his Markdown document; you could also use it to get custom structural elements with custom 'translations' into your document.

(The plain Markdown output)
<#define rightpic|![#1](#2)>
(The output for HTML mode)
<#ifdef HTML>
    <#define rightpic|<img alt="#1" href="#2" float="right" />> 
<#endif>
(the output for TeX mode)
<#ifdef TEX> 
    <#define rightpic|\\placefigure[right]{#1}{\\externalfigure[#2]}> 
<#endif> 

I have here a picture of a cow. <#rightpic My caption|cow>

Compile this directly to context or HTML with

gpp -H -DTEX=1  mysource.md | pandoc -f markdown -t context -o myresult.tex
gpp -H -DHTML=1 mysource.md | pandoc -f markdown -t html    -o myresult.html

Good luck!

  • thanks! I'll definitely look into gpp, will leave the accepted answer for the folks coming from a search though. – mb21 Jul 23 '13 at 16:31

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.