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?
> 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?
> 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?
> 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?
> 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
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.
> 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{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
\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
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}
Jim Hefferon wrote: > 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.
> \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
> 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}
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.
>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
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.
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. -- Robin Fairbairns, Cambridge -- rf10 at cam dot ac dot uk
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.Kast...@t-online.de
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.
>> 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... -- Robin Fairbairns, Cambridge -- rf10 at cam dot ac dot uk
> ... 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.