Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

display programming source file

145 views
Skip to first unread message

nurbz

unread,
Jan 2, 2002, 8:43:17 AM1/2/02
to
Hi all,

I am using programming quite a lot in my studies, so I always have to add
some source code in my LaTeX report. But I am just using an awful 'verb'
environment to display them. Is there any package to display easily
source file, such as C/C++, matlab, java, and so on?
Furthermore, is there any package that can colorize automatically C/C++
source file?

Thanks in advance
Alexis

wald

unread,
Jan 2, 2002, 7:55:40 AM1/2/02
to
A look at the FAQ wouldn't hurt:

http://www.tex.ac.uk/cgi-bin/texfaq2html?keyword=&question=123

Greetz,
Wald

"nurbz" <nur...@ematic.com> wrote in message
news:pan.2002.01.02.1...@ematic.com...

Herbert Voss

unread,
Jan 2, 2002, 8:38:20 AM1/2/02
to
nurbz wrote:

have a look at
http://www.lyx.org/help/listings/listings.php3

Herbert

ptk

unread,
Jan 2, 2002, 10:38:06 AM1/2/02
to

ptk

unread,
Jan 2, 2002, 10:38:21 AM1/2/02
to
nurbz wrote:

I apologize for perhaps missing the obvious, but how do you get the listings package to colorize the source code? This same question has appeared numerous times with the same link to the FAQ. It would really be nice to see an example.

thanks in advance


Jim Hefferon

unread,
Jan 2, 2002, 1:36:06 PM1/2/02
to
ptk <p...@dont.spam.com> wrote

> I apologize for perhaps missing the obvious, but how do you get the listings package to colorize the source code? This same question has appeared numerous times with the same link to the FAQ. It would really be nice to see an example.

Here are two .sty files, and sample input that work for me; I think
that
you need to have a recent `listings' for this to go.

I have trouble viewing the colored text in GhostScript so I convert
to PDF instead.

HTH,
Jim

=====ham_notes.sty=======
%\RequirePackage{dvidrv}
\RequirePackage{color}
%\definecolor{mediumblue}{rgb}{0.00,0.00,0.804}
\definecolor{chocolate}{rgb}{0.82,0.41,0.18} % color 176
\definecolor{pythoncolor}{rgb}{0.82,0.41,0.18}

\definecolor{darkolivegreen}{rgb}{0.333,0.420,0.184} %color 116 (GI
color?)
\definecolor{sqlcolor}{rgb}{0.333,0.420,0.184} %color 116 (GI color?)

\definecolor{sienna}{rgb}{0.627,0.322,0.176} %color 168 (coffee
color?)
\definecolor{javacolor}{rgb}{0.627,0.322,0.176} %color 168

\definecolor{stringcolor}{rgb}{0.25,0.25,0.25}
\definecolor{textcolor}{rgb}{0.25,0.25,0.25}

%\definecolor{indianred3}{rgb}{0.804,0.333,0.333} %color 414
\definecolor{indianred3}{rgb}{0.933,0.388,0.388} %color 413
\definecolor{htmlcolor}{rgb}{0.933,0.388,0.388} %color 413

\definecolor{mediumseagreen}{rgb}{0.477,0.702,0.441} %color 122
\definecolor{shellcolor}{rgb}{0.477,0.702,0.441} %color 122

\definecolor{brown}{rgb}{0.647,0.165,0.165} % color 178
\definecolor{slateblue4}{rgb}{0.278,0.235,0.545} % color 279
\definecolor{perlcolor}{rgb}{0.278,0.235,0.545}
%\definecolor{shellcolor}{rgb}{0.0,0.25,0.0}
\definecolor{htmlcolor}{rgb}{0.99,0,0} % red
\definecolor{lightslategrey}{rgb}{0.467,0.533,0.600} %color 52
\definecolor{xmlcolor}{rgb}{0.99,0,0} % red
\definecolor{goldenrod}{rgb}{0.855,0.647,0.125} %color 159
%\definecolor{sqlcolor}{rgb}{0.071,0.416,0.173} % a dark green

\usepackage{listings}
\lstloadlanguages{Python,SQL,csh,HTML}

\lstdefinestyle{sql}{language=SQL,
%labelstyle=\tiny,labelstep=5,labelsep=7pt,
keywordstyle=\color{sqlcolor},
ndkeywordstyle=\color{sqlcolor},
identifierstyle=\color{sqlcolor},
stringstyle=\color{sqlcolor}}
\lstdefinestyle{python}{language=Python,
%labelstyle=\tiny,labelstep=5,labelsep=7pt,
keywordstyle=\color{pythoncolor},
ndkeywordstyle=\color{pythoncolor},
identifierstyle=\color{pythoncolor},
stringstyle=\color{pythoncolor}}
\lstdefinestyle{text}{language=,
keywordstyle=\color{textcolor},
ndkeywordstyle=\color{textcolor},
identifierstyle=\color{textcolor},
stringstyle=\color{textcolor}}
\lstdefinestyle{shell}{language=csh,
keywordstyle=\color{shellcolor},
ndkeywordstyle=\color{shellcolor},
identifierstyle=\color{shellcolor},
stringstyle=\color{shellcolor}}
\lstdefinestyle{html}{language=HTML,
keywordstyle=\color{htmlcolor},
ndkeywordstyle=\color{textcolor},
identifierstyle=\color{textcolor},
stringstyle=\color{textcolor},
keywordsinside=<>}
\AtBeginDocument{%
\lstset{%
indent=15pt,
basicstyle=\ttfamily\small,
%keywordstyle=\color{blue},
%ndkeywordstyle=\color{blue},
%identifierstyle=\color{blue},
%stringstyle=\color{blue},
stringspaces=false}}
=====lstlang0.sty====
% lstlang0.sty
% language definitions for the listings package
\lst@definelanguage{text}%
{keywords={},ndkeywords={}}
====lst_test.tex===
\documentclass{book}
\usepackage{color}
\usepackage{ham_notes}
\usepackage{url}
\usepackage{hyperref}

\newcommand{\pg}{PostgreSQL}

\title{KE1AZ's Internet Linux Log}
\author{Jim Hefferon}

\begin{document}
\maketitle

\chapter{Making the Database}


\section{Making the State Database}
\href{http://www.google.com}{Google} found
this file of state and possession abbreviations that
on the web site of the US Postal Service.
\begin{lstlisting}[style=text]{}
State/Possession Abbreviation

ALABAMA AL
ALASKA AK
AMERICAN SAMOA AS
ARIZONA AZ

..

WEST VIRGINIA WV
WISCONSIN WI
WYOMING WY


Military "State" Abbreviation

Armed Forces Africa AE
Armed Forces Americas AA
(except Canada)
Armed Forces Canada AE
Armed Forces Europe AE
Armed Forces Middle East AE
Armed Forces Pacific AP
\end{lstlisting}
I edited out the two title lines
(`Military~\ldots' and `State/Possession~\ldots') along with the blank
lines,
and adjusted the `except Canada' line.
Also,
I don't know anything about the postal codes for the military cases,
but I see that three of them share a designation.
I combined those three.

The \url{create_states.sql} script file
\begin{lstlisting}[style=sql,firstlabel=1]{}
CREATE TABLE state_types (
type text,
PRIMARY KEY(type)
);

INSERT INTO state_types (type) VALUES ('state');
INSERT INTO state_types (type) VALUES ('possession');
INSERT INTO state_types (type) VALUES ('military');

CREATE TABLE states (
abbrev CHAR(2) NOT NULL,
name text NOT NULL,
status text,
PRIMARY KEY(abbrev),
FOREIGN KEY(status) REFERENCES state_types
);
\end{lstlisting}
run from the command line
\begin{lstlisting}[style=shell]{}
[jim@millstone pg]$ psql jim -a -fcreate_states.sql
\end{lstlisting}
will make the two tables.
The \verb!state_types! is how I limit
\verb!states.status! to only those three values.

Note that I had to create the \verb!state_types! first or else
\pg\ objects to referencing them as foreign keys.

Next I need to get the data into the database.
Then this Python script
\begin{lstlisting}[style=python]{}
#!/usr/bin/python
# read_states.py
# Read the list of states and abbreviations
# 20-Nov-2001 Jim Hefferon j...@joshua.smcvt.edu
import string

infile_name="abbr_state.txt"
infile=open(infile_name,'r')
outfile_name="populate_states.sql"
outfile=open(outfile_name,'w')

for x in ('state','possession','military'):
outfile.write("INSERT INTO state_types (type) VALUES ('%s');\n" %
(x,))

while 1:
line=infile.readline()[:-1]
if not line:
break
fields=string.split(line)
outfile.write("INSERT INTO states (abbrev,name,status) VALUES
('%s','%s','%s');\n" %
(fields[-1],string.capwords(string.join(fields[0:-1])),'state'))
infile.close()
outfile.close()
\end{lstlisting}
run from the command line
\begin{lstlisting}{text}
[jim@millstone pg]$ ./read_states.py
\end{lstlisting}
gave me a file of \verb!INSERT! statements.
I've decided to produce the SQL script file.
\begin{lstlisting}[style=sql]{}
INSERT INTO states (abbrev,name,status) VALUES
('AL','Alabama','state');
INSERT INTO states (abbrev,name,status) VALUES
('AK','Alaska','state');
INSERT INTO states (abbrev,name,status) VALUES ('AS','American
Samoa','possession');
...
INSERT INTO states (abbrev,name,status) VALUES
('WY','Wyoming','state');
INSERT INTO states (abbrev,name,status) VALUES ('AE','Armed Forces
Africa, Canada, Europe, And The Middle East','military');
INSERT INTO states (abbrev,name,status) VALUES ('AA','Armed Forces
Americas (except Canada)','military');
INSERT INTO states (abbrev,name,status) VALUES ('AP','Armed Forces
Pacific','military');
\end{lstlisting}
Note that, as produced by the script, everything is a `state', but I
edited it by hand to change some to `possession' or `military'.
(I called Washington DC a `possession' even though it didn't seem
right,
since it isn't either of the other two.)
\end{document}

ptk

unread,
Jan 2, 2002, 6:02:30 PM1/2/02
to
Jim Hefferon wrote:

Thanks a bunch - I know this has help everyone much more than the usual
remarks about the FAQ, or even worse the pedantic and sarcastic remarks
about RTFM.

Robin Fairbairns

unread,
Jan 2, 2002, 6:47:07 PM1/2/02
to
ptk <p...@dont.spam.com> wrote:
>Jim Hefferon wrote:
>>[worked-out solution, quoted in full]

>
>Thanks a bunch - I know this has help everyone much more than the usual
>remarks about the FAQ, or even worse the pedantic and sarcastic remarks
>about RTFM.

ah. so the faq is unuseful? or never solves problems? which?

do you think i should stop wasting my time?

face it, idiot-who-can't-even-trim-posts, you get told to rtfm if the
question is so trivial that anyone who bothers to read anything should
know it.

you get pointed to someone's faq if it answers the question.

you get pointed to a package if that solves the problem.

you *only* get a worked solution from someone who's done the work (or
knows how) and who believes there's no existing packaged solution.

which (i believe) is the case here. which is why i didn't point you
to my answer about listing packages, or to a different package.

if you don't like the way people answer your questions, why bother
asking?
--
Robin Fairbairns, Cambridge -- rf10 at cam dot ac dot uk

ptk

unread,
Jan 2, 2002, 7:00:37 PM1/2/02
to
No I didn't say your faq was useless & yes I, and I'm sure everyone else
that has asked the question, has looked at your FAQ & is indeed indebted
to you for your unending efforts. However, your FAQ only gave cursory
attention to the issue of source code coloring, as I'm sure you're more
than aware. I have seen numerous posts on this same subject in just the
past two months - some even answered by your self. If indeed the answer
is so trivial, I and the rest of the lesser individuals are a little
perplexed as to why you didn't answer the question about HOW TO COLORIZE
program source, NOT SIMPLY POINT TO THE LISTING PACKAGE - such is the
confusion of the least.

Robin Fairbairns

unread,
Jan 3, 2002, 5:19:15 AM1/3/02
to
ptk <p...@dont.spam.com> wrote:
>No I didn't say your faq was useless

you merely said answers that suggested looking at the faq were.

>& yes I, and I'm sure everyone else
>that has asked the question, has looked at your FAQ & is indeed indebted
>to you for your unending efforts. However, your FAQ only gave cursory
>attention to the issue of source code coloring, as I'm sure you're more
>than aware. I have seen numerous posts on this same subject in just the
>past two months - some even answered by your self.

i am aware of answering one question about source code colouring
saying i knew of no way to do it, but that a summary of what i _did_
know was in the faq answer.

>If indeed the answer
>is so trivial, I and the rest of the lesser individuals are a little
>perplexed as to why you didn't answer the question about HOW TO COLORIZE
> program source, NOT SIMPLY POINT TO THE LISTING PACKAGE - such is the
>confusion of the least.

it plainly isn't trivial. there are admittedly rather few people who
bother to answer questions here, but the number is not entirely
insignificant. only one of them (possibly after experimentation
following an earlier post) had a working solution: all the others
merely either mis-read your post, or (in my case) realised they didn't
have an answer.

i suppose you quoted the previous post in full because you thought it
was trivial?

plonk.

ptk

unread,
Jan 3, 2002, 8:28:43 AM1/3/02
to
please forgive me for doubting you, your excellency

David Kastrup

unread,
Jan 3, 2002, 9:17:33 AM1/3/02
to
ptk <p...@dont.spam.com> writes:

> please forgive me for doubting you, your excellency

Your sarcasm would probably be better appreciated if people saw as
much helpfulness from you on TeXnical matters as from Robin. I cannot
remember your entire posting history to contain as much helpful advice
as even only Robin's unthanked for attempts of helping you in this
particular thread.

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Email: David....@t-online.de

Peter Flynn

unread,
Jan 3, 2002, 6:34:15 PM1/3/02
to
ptk wrote:

> No I didn't say your faq was useless & yes I, and I'm sure everyone else
> that has asked the question, has looked at your FAQ & is indeed indebted
> to you for your unending efforts. However, your FAQ only gave cursory
> attention to the issue of source code coloring, as I'm sure you're more


Hardly surprising: it deals with FREQUENTLY-asked questions only.


> than aware. I have seen numerous posts on this same subject in just the
> past two months - some even answered by your self. If indeed the answer


"Numerous" within two months is probably not frequently enough, I
suspect (compared with the frequency of other questions).


> is so trivial, I and the rest of the lesser individuals are a little
> perplexed as to why you didn't answer the question about HOW TO COLORIZE
> program source, NOT SIMPLY POINT TO THE LISTING PACKAGE - such is the
> confusion of the least.


If you would stop for a moment to think about what it is you have asked
you'll realise that source code colorisation is a highly non-trivial
task. In effect it either means implementing an entire C/C++ parser in
LaTeX, or -- for a lower syntactical level -- using regexps like Emacs
does. Neither of these comes even remotely within the remit of LaTeX as
a typesetting package, and although it is probably possible to do the
task (as Jim Hefferon has already posted) I don't think it is high on
anyone's list. Indeed, if you go and read what Knuth says about using
TeX to format his own Pascal code (different language, and in bold and
italic, not colour, but the principles are the same), you might get some
understanding of the difficulty of the task.

If you crack it, especially if you crack it in a generic manner which
can be applied to any programming language, please let us know.

///Peter

Robin Fairbairns

unread,
Jan 4, 2002, 2:48:52 AM1/4/02
to
Peter Flynn <pe...@silmaril.ie> wrote:
>ptk wrote:
>
>> No I didn't say your faq was useless & yes I, and I'm sure everyone else
>> that has asked the question, has looked at your FAQ & is indeed indebted
>> to you for your unending efforts. However, your FAQ only gave cursory
>> attention to the issue of source code coloring, as I'm sure you're more
>
>Hardly surprising: it deals with FREQUENTLY-asked questions only.

the other requirement is that i know an answer.

being (as i am) a sysadmin, i don't have much call for typesetting
code nowadays (and none of my users has ever asked for help in the
area). i only do worked examples for c.t.t on a whim, and no such
object has ever troubled me in respect of source code colouring...

Jim Hefferon

unread,
Jan 4, 2002, 9:48:09 AM1/4/02
to
Peter Flynn <pe...@silmaril.ie> wrote
> ... and although it is probably possible to do the

> task (as Jim Hefferon has already posted) I don't think it is high on
> anyone's list.
Please, Carsten Heinz did the work (based on the verbatim package, of course,
so there is scads of fine work there, too). He's really done a wonderful job,
within the limits Peter describes. I just doped out the manual.

Jim

0 new messages