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.
\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
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]}}
#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
\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
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|>
(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!
gpp, will leave the accepted answer for the folks coming from a search though.
– mb21
Jul 23 '13 at 16:31
\def- I've never used ConTeXt, but\defwill always overwrite a definition in Plain TeX. Have you tried simply\defineing it again? – Sean Allred Jul 22 '13 at 13:39\externalfigure. – Aditya Jul 22 '13 at 14:18\externalfigures in the body text. I can only change the surrounding code in the template. – mb21 Jul 22 '13 at 14:20pandocgenerates floating figures by default. (You need to leave a newline after the figure specification). – Aditya Jul 22 '13 at 17:05\placefigure[here...but I neededrightinstead ofhere. – mb21 Jul 22 '13 at 17:17