[metapost] Multiple line LaTeX

Steve Checkoway s at pahtak.org
Tue Feb 17 15:00:57 CET 2009

The latexMP package is a great way to include a single line of LaTeX  
in a MetaPost file but when multiple lines are desired, this doesn't  
quite work.

The particular use case I had was where verbatim like text was needed.  
In particular:

ld sp,hl

Now it's possible that I could have played games with something like:

{\catcode`\X=5 \begin{lstlisting}Xld sp,hlXretX\end{lstlisting}}

But that's fairly ugly. Another solution would have been to write a  
separate file and use \lstinput{foo} (or whatever that particular  
macro is). However, it occurred to me that this could be done in a  
fairly general way. The following is a quick hack that I think would  
be useful for someone who's been using MetaPost longer than me (read:  
longer than a week) to clean up and possibly include in latexMP itself.

(You'll have to forgive my unimaginative variable names, ml is  
supposed to be for multiline.)

string latexmp_aux; latexmp_aux := "ltx-" & jobname & ".aux";
string latexmp_ml[];
numeric latexmp_mlcounter; latexmp_mlcounter := -1;

def texmultiline( text ss ) =
         latexmp_mlcounter := latexmp_mlcounter + 1;
         latexmp_ml[latexmp_mlcounter] := "";
         for s=ss :
                 if latexmp_ml[latexmp_mlcounter] = "" :
                         latexmp_ml[latexmp_mlcounter] := s;
                 else :
                         latexmp_ml[latexmp_mlcounter] :=  
latexmp_ml[latexmp_mlcounter] & char(10) & s;
         textext( "\def\latexmpmlcount{" & decimal(latexmp_mlcounter)  
& "}\input{" & latexmp_aux & "}" )

def writetexmultiline =
         if latexmp_mlcounter >= 0 :
                 write EOF to latexmp_aux;
                 write "\expandafter\ifcase\expandafter\latexmpmlcount 
\expandafter{%" to latexmp_aux;
                 write latexmp_ml0 to latexmp_aux;
                 for n=1 upto latexmp_mlcounter :
                         write "}\expandafter\or\expandafter{%" to  
                         write latexmp_ml[n] to latexmp_aux;
                 write "}\else multiline \LaTeX\fi" to latexmp_aux;
                 write EOF to latexmp_aux;
let origShipit_latexmpml = shipit;
let shipit = writetexmultiline;

As an example of the usage, foo.mp contains:

input latexmp;
input latexmphelper;% The above code and a call to setupLaTeXMP
% The vbox is because \shipout\hbox is used to write the text.
label( texmultiline("\vbox{\begin{verbatim}",
		    "This is some",
		    "verbatim text.",
		    "}"), (0,100) );
label( texmultiline("\begin{lstlisting}",
		    "ld sp,hl",
		    "\end{lstlisting}"), (0,50) );
label( texmultiline("\def\blah{4}%",
		    "\ifnum\blah=0 zero\else nonzero\fi"), origin );

Running mpost foo produces (among other things) ltx-foo.tmp where the  
relevant lines here are:

picture latexmp_picture[];
latexmp_picture[1]:= btex \def\latexmpmlcount{0}\input{ltx-foo.aux}  
etex ;
latexmp_picture[2]:= btex \def\latexmpmlcount{1}\input{ltx-foo.aux}  
etex ;
latexmp_picture[3]:= btex \def\latexmpmlcount{2}\input{ltx-foo.aux}  
etex ;

as well as ltx-foo.aux which is input thrice:

This is some
verbatim text.
ld sp,hl
\ifnum\blah=0 zero\else nonzero\fi
}\else multiline \LaTeX\fi

Running mpost foo again correctly generates this picture. The  
gibberish with the \expandafters is to get TeX to see the \or, \else  
and \fi before expanding the other parts incase there are other \or,  
\else, or \fi. It's possible the brackets are sufficient or perhaps  
neither are needed. I'd need to go through the TeXbook with a fine  
toothed comb to be sure exactly when and how TeX will expand \ifcase  
and friends.

Steve Checkoway

     "Anyone who says that the solution is to educate the users
     hasn't ever met an actual user." -- Bruce Schneier

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2407 bytes
Desc: not available
Url : http://tug.org/pipermail/metapost/attachments/20090217/e30839bb/attachment.bin 

More information about the metapost mailing list