# [texworks] xargs / lstlistings quoting of arguments

Paul A Norman paul.a.norman at gmail.com
Sun Oct 11 00:13:36 CEST 2009

Dear Sam,
You are wanting to publish your working code through Latex?

Trusting I have grasped your predicament correctly ...

I'm not sure quite how this might work with you using a batch mode
(consecutive file input?), and you may need to preprocess the files as well
to cloth them with the $sign ... -- p.54 of the listings package manual reads: "The listings package defines the mathescape key, which lets ‘$’ escape to TEX’s math mode."

command prompt:> texdoc listings

Are you perhaps able to ulitise this during a pre-process, to clothe any
difficult characters to allow your needed string literals?

As another route ... I have effectively used even Php5 cli scripts on
the command line to process data through a personally defined LaTeX
template, generating a new LaTeX document conforming to my needs.  Then
process through TeXWorks.

In the future through scripting, you will even possibly be able to directly
achieve this through TeXWorks' scripting engine(s) ;-)

Think of it as a mail merge of sorts.  In it you can use maths
$sometimes to 'escape' the difficult characters. (set an array of them in your scripting language of choice :) and either REGEXPRESSION or "cloth" through simple string replace, encountered members of the array with the maths$ or other treatment as needed.  Source can be your files from disk,
database or whatever,  result in a beautiful LaTeX set of documents ready to
process to make beautiful code listings, or at least hopefully input file(s)
that will work for you !

As I said above - Trusting I have grasped your predicament correctly ...

Paul

2009/10/10 Sam Liddicott <sam at liddicott.com>

> 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 --------------
An HTML attachment was scrubbed...