[texhax] xargs / lstlistings quoting of arguments

Sam Liddicott sam at liddicott.com
Sat Oct 10 08:43:57 CEST 2009


(notangle and noweave are literate programming tools.)

I'm just finishing a re-write of notangle in awk and noweave in latex.
Interested parties can see git tree at http://repo.or.cz/w/newfangle.git
(I use Lyx as my authoring tool).

I have some difficulties with advanced escaping and arguments.

Another chunk may be referred to with this command, with optional
arguments passed as chunk parameters - a bit like C macros.

\chunkref{chunkname}[optional, arguments, here]

I'm having some trouble with arguments to a chunk, when the arguments
might contain and underscore or a back-slash

I'm using the listings package \lst at ReplaceIn\chunk\lst at filenamerpl 
whose purpose is to escape out certain special characters, and that 
works fine enough except in some complex cases I'll describe.

If I want to pass an array element as a chunk parameter I might have
some code that looks like this:

\begin{lstlistings}
   int s_int[10];
   x=3;
   \chunkref{do_something}[s_int[x]]
\end{lstlistings}

\lst at ReplaceIn will fixup the _ in s_int for me.

of couse the ] in [x] will close the xargs optional argument, so instead
I can do:
   \chunkref{do_something}[{s_int[x]}]
which works fine, and if I have a second parameter I would do:
   \chunkref{do_something_more}[{s_int[x], y}]
which works fine too at the moment.

but then image it is a 2D array;
   \chunkref{do_something_more}[{s_int[x, 2], y}]

the , in [x, 2] would technically signify the start of the second
parameter, so I could enclose each argument in { }
   \chunkref{do_something_more}[{s_int[x, 2]}, {y}]

but either xargs cat codes or listings \lst at ReplaceIn chokes on any { }
which don't enclose the whole argument, and so the underscore in s_int
is not escaped any more.

I attach a minimal sample file newfangle.txt which show how additional
inner braces prevent the escaping of enclosed underscores and other
characters.

I suppose I am inviting discussion on these aspects:
1. how I might properly escape inner underscores (and backslashes, etc)

2. why I'm trying to let the author type (almost) plain C in the middle
of a latex document anyway (but thats what lstlistings lets you do)

3. if there are better methods to present parameterised code chunks
-   one idea that appeals to me is to take the xargs optional argument
which is all the parameters and then apply that to a function which is
looking for comma separated arguments and will eat any { } (providing
xargs hasn't changed the cat-code).

Maybe I should not call \lst at ReplaceIn on the entire xargs argument 
(which is multiple chunk arguments) but first split it into multiple 
arguments with a recursive function that looks for arguments ending in a 
comma, and then apply \lst at ReplaceIn on each one... it will take me all 
morning but I'll try it.
I've read Knuths ("you can't pdf it") tex book and the other one and 
blown my brain a few times, but I still don't have the answers.

I'll be glad for any enlightenment that helps me on my way.

Sam
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newfangle.tex
Type: text/x-tex
Size: 677 bytes
Desc: not available
URL: <http://tug.org/pipermail/texhax/attachments/20091010/bc31dfd2/attachment-0001.bin>


More information about the texhax mailing list