[texworks] xargs / lstlistings quoting of arguments

Sam Liddicott sam at liddicott.com
Fri Oct 9 22:40:50 CEST 2009


I'm just finishing a re-write of notangle/noweave in awk/latex.
(interested parties can see git tree athttp://repo.or.cz/w/newfangle.git)

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.

I'm using lstlistings \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, but and if I have a second parameter I would do:
  \chunkref{do_something_more}[{s_int[x], y}]

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 (although the { } are not preserved in the output

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
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).

I've read Knuths (you can't pdf me) 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newfangle.tex
Type: text/x-tex
Size: 676 bytes
Desc: not available
URL: <http://tug.org/pipermail/texworks/attachments/20091009/61b2bfcf/attachment-0001.bin>


More information about the texworks mailing list