Inclusion of program listing

berntl's picture

What is the best way to include program listing, where I get decent typesetting?

I have used verbatim so far, but the result is not very nice -- e.g. tabs become \qquad or something.

I seem to recall that George suggested the latex package "listings" a while ago. But I don't recall the description of how to activate/use it. Is this documented somewhere? And will it also work for SWP 6?

Also, I'm using a modeling language named Modelica, and I don't think "listings" has support for that one. Is there a way I can add definitions of a new language (such as Modelica)?

-B

Barry MacKichan's picture

I'm attaching a document that

I'm attaching a document that shows how to import an external file as a program listing using the "listings" package. It is possible to type your program code in directly, but it is frustrating because we don't handle the cursor correctly except in the verbatim environment. I think I know how to correct this, but I haven't yet.

The attached document should get you going, and there are two .frg files that can help. I am attaching those also. They go into the fragments directory in your user profile (where this is is platform dependent).

The initListings TeX button contains a 'definition' for the JavaScript language which is not directly supported by "listings'. I think you could do something very similar for Modelica.

 

Barry MacKichan's picture

I think you'll find that in

I think you'll find that in version 6.0.14 that the problem of '\qquad' and '~'  appearing in verbatim text has been fixed.

If we had a new tag for listings, would you want to have it appear as verbatim on the screen?

—Barry

 

berntl's picture

Yes, a tag for listings would

Yes, a tag for listings would be very, very useful: I'm sure I'm not the only one of SWP users who combine text, math, figures/plots, and computer listing in documents. A tag supporting the following would be fantastic:

a. Possibility to insert code from a specified file, and show specified line numbers (line no. m to line no. n).
b. Possibility with syntax highlighting for various languages (boldface keywords and/or color highlighting for keywords, etc.).
c. Possibility to "box" the code.
d. Possibility with a caption.

The most important would be a, c, d, but inclusion of b would let it look more professional.
--
In a future version (SWP 7?), the possibility to include Jupyter notebooks would be superb. Jupyter notebooks are in some ways similar to SWP -- but support live numeric code instead of symbolic languages. Jupyter is an outgrowth from iPython notebook (probably modeled on Mathematica notebooks), and originally supported languages Julia, Python and R. Now, a large number of languages are supported. Jupyter allows for structuring of documents, and simple math typesetting (using LaTeX syntax).

Jupyter notebooks run in browsers, and I think it is possible to save Jupyter notebooks as XHTML. Perhaps it would be possible to include such XHTML code in SWP? Of course, the ultimate would be to keep imported Jupyter notebooks live, and enable the user to save code back to Jupyter, I guess. But I realize that this is not the first priority.

Barry MacKichan's picture

Thanks for the suggestions.

Thanks for the suggestions. I'll look at them as time allows.

—Barry

 

tomprice's picture

I have used the listings

I have used the listings package with straight LaTeX markup. It is a versatile package, which produces excellent output. I would probably use a TeX field if I wanted to add a listing to an SWP file. You can find the documentation at
http://ctan.org/pkg/listings

berntl's picture

Thanks Tom (and apologies for

Thanks Tom (and apologies for two postings of the same topic... sometime posting takes forever, and I click the save button several times... :-( ).

OK, here is the basic info on the page you give...
"To use, \usep­a­ck­age{list­ings}, iden­tify the lan­guage of the ob­ject to type­set, us­ing a con­struct like: \lst­set{lan­guage=Python}, then use en­vi­ron­ment lstlist­ing for in­line code. Ex­ter­nal files may be for­mat­ted us­ing \lstin­put­list­ing to pro­cess a given file in the form ap­pro­pri­ate for the cur­rent lan­guage. Short (in-line) list­ings are also avail­able, us­ing ei­ther \lstin­line|...| or |...| (af­ter defin­ing the | to­ken with the \lstMakeShortIn­line com­mand)."

Since I'm not that good with raw LaTeX...
* Do I have to download some package/LaTeX file(s)? Where do I have to place them on my computer?
* Where do I place the statement "\usepackage{listings}"? Can I use the Typeset/Options and Packages.../Add functionality of SWP after I have downloaded the package, can I put the statement in the Preamble, or where do I put it?
* The remaining statements ("\lstset{language=Python}", etc.), I assume that I can put in a TeX field (Encapculated or unEncapsulated? I'm not quite sure about the effect of the two options...)?

-B

tomprice's picture

The following is what I would

The following is what I would try using SWP 6.0. I suggest you use a COPY of your file and not the original.
In the SWP editor navigate to Typeset > Options and Packages. This opens the Options and Packages dialog. Scroll to the right and click on Go Native. This will open the Native LaTeX Packages dialog. Add {listings}, including the braces, to the Packages field. Click "OK" twice (I think).

Now go to the LaTeX Preamble dialog by clicking on Typeset > Preamble. Add something like the following at the bottom of the text entry field. (The manual describes the options.)
\lstset{% general command to set parameter(s)
basicstyle=\small, % print whole listing small
keywordstyle=\color{black}\bfseries\underbar, % underlined bold black keywords
identifierstyle=, % nothing happens
commentstyle=\color{white}, % white comments
stringstyle=\ttfamily, % typewriter type for strings
showstringspaces=false} % no special string spaces

Now, in the body of your document you can have a listing like the following. Read the listings documentation to learn the best way to enter a caption.
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
WritE('Pascal keywords.');
\end{lstlisting}

which should produce something like in the ttfamily
for i :=maxint to 0 do
begin

end;

Write( 'Case insensitive ' ) ;
WritE( 'Pascal keywords.' ) ;

This package has many powerful features.

berntl's picture

I should say: I'll probably

I should say: I'll probably test it using SWP 5.5 first. I haven't really gotten started to use SWP 6 yet. Why? Because SWP 6 still doesn't support non-US/non-English keyboards. In other words: keyboard shortcuts don't work if the symbol is located on a different key location in my Norwegian keyboard than in the US/English keyboard.

I think this problem was reported fixed in SWP 6.0.2/3 or so, but it still doesn't work on my computers.

tomprice's picture

You will need to add the

You will need to add the listings package to your formatting system if you use TrueTeX, the default system for SWP 5.5. I am not familiar with the algorithm and algorithmic packages, but they are a part of the SWP 5.5 TrueTeX formatting system. You may find one of these two acceptable.

tomprice's picture

You will need to add the

You will need to add the listings package to your formatting system if you use TrueTeX, the default system for SWP 5.5. I am not familiar with the algorithm and algorithmic packages, but they are a part of the SWP 5.5 TrueTeX formatting system. You may find one of these two acceptable.

berntl's picture

Thanks, Tom. I'll try this on

Thanks, Tom.

I'll try this on a simple document..., not my 400 p lecture notes... :-)

"Now, in the body of your document you can have a listing like the following. Read ..."

Does it matter whether it is Encapculated or Unencapculated TeX field? OK -- I can probably play around a little...

tomprice's picture

Sorry, I intended to indicate

Sorry, I intended to indicate that listings environments should go in an encapsulated TeX field.

tomprice's picture

I have used the listings

I have used the listings package with straight LaTeX markup. It is a versatile package, which produces excellent output. I would probably use a TeX field if I wanted to add a listing to an SWP file. You can find the documentation at
http://ctan.org/pkg/listings