Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using the listings package, I am trying to input a Java source file into my document using a specified, user-defined style.

This works just fine if I have the specified style definition in the same document. However, I wish to save the style so that I can use it without having the definition directly in my document.

Section 2.4.1 of the listings manual sounded promising, so I attempted to save my style in a file called listings-java.prf in the same directory as the listings.cfg file itself. I then added \input{listings-java.prf} at the bottom of listings.cfg. However, when I try to use the style, I get an error saying that it cannot load the requested style for whatever reason. It's the same error I get if I try to specify a style that doesn't exist, so I assume that it is not even trying to read my file or else my file incorrectly written/specified/otherwise linked to.

The guide says to ensure the file is located within TeX's search path, and I assume that the directory containing listings.cfg is in that path, which is why I placed it there.

What am I doing wrong, and how can I fix it?

EDIT: Following the suggestion in comments, I have shifted my \input command up a line, directly before the \endinput command in listings.cfg. Now, I get the following error (despite listings-java.prf being in the same directory):

(E:\non-modeling\LaTeX\tex\latex\listings\listings.sty
(E:\non-modeling\LaTeX\tex\latex\listings\lstmisc.sty)
(E:\non-modeling\LaTeX\tex\latex\listings\listings.cfg

! LaTeX Error: File `listings-java.prf' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: prf)

So it would appear that the same directory does not count as being in TeX's search path. How can I add it?

When I manually input the absolute path to the file, it proceeds to read it, but then tells me that \definecolor is an undefined control sequence. I tried adding \usepackage{xcolor} to the top of the file, but then I get errors about that package already being in use (because I also have it at the top of my document file). Can this be fixed, or do I need to inline all references to the defined colors?

Here's listings-java.prf:

\definecolor{bg}{RGB}{25,25,25}       % code background color
\definecolor{fg}{RGB}{166,166,166}    % code foreground color
\definecolor{kw}{RGB}{194,77,67}      % keyword color
\definecolor{ct}{RGB}{80,84,79}       % comment color
\definecolor{st}{RGB}{185,185,114}    % string color
\definecolor{cs}{RGB}{248,248,242}    % classname color
\definecolor{hl}{RGB}{71,148,99}      % highlighting color
\definecolor{at}{RGB}{253,151,31}     % annotation color

\lstdefinestyle{Java}{
    language=Java,                  % choose the language of the code
    basicstyle=\footnotesize        % the style of the fonts that are used for the code
               \ttfamily
               \color{fg},
    numbers=left,                   % where to put the line-numbers
    numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
    stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
    numbersep=5pt,                  % how far the line-numbers are from the code
    backgroundcolor=\color{bg},     % choose the background color
    showspaces=false,               % don't show spaces by adding particular underscores
    showstringspaces=false,         % don't underline spaces within strings
    showtabs=false,                 % don't show tabs within strings by adding particular underscores
    frame=single,                   % adds a frame around the code
    rulecolor=\color{BrickRed},
    tabsize=2,                      % sets default tabsize to 2 spaces
    captionpos=b,                   % sets the caption-position to bottom
    keywordstyle=\color{kw}\bf,     % the style of the font used for keywords
    commentstyle=\color{ct},        % the style of the font used for comments
    stringstyle=\color{st},         % the style of the font used for strings
    breaklines=true,                % sets automatic line breaking
    breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
    escapeinside=\`\`,              % if you want to jump out to LaTeX within the code
    moredelim=**[is][\color{hl}\bf]{@@}{@@} % delimeters for highlighting pieces of code
}

I get the same error even if I remove all information concerning color and have only the style definition in the file.

Here's listings.cfg:

%%
%% This is file `listings.cfg',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lstdrvrs.dtx  (with options: `config')
%% 
%% The listings package is copyright 1996--2004 Carsten Heinz, and
%% continued maintenance on the package is copyright 2006--2007 Brooks
%% Moses. From 2013 on the maintenance is done by Jobst Hoffmann.
%% The drivers are copyright 1997/1998/1999/2000/2001/2002/2003/2004/2006/
%% 2007/2013 any individual author listed in this file.
%%
%% This file is distributed under the terms of the LaTeX Project Public
%% License from CTAN archives in directory  macros/latex/base/lppl.txt.
%% Either version 1.3 or, at your option, any later version.
%%
%% This file is completely free and comes without any warranty.
%%
%% Send comments and ideas on the package, error reports and additional
%% programming languages to Jobst Hoffmann at <j.hoffmann@fh-aachen.de>.
%%
\ProvidesFile{listings.cfg}[2014/03/04 1.5c listings configuration]
\def\lstlanguagefiles
    {lstlang0.sty,lstlang1.sty,lstlang2.sty,lstlang3.sty}
\lstset{defaultdialect=[R/3 6.10]ABAP,
        defaultdialect=[2005]Ada,
        defaultdialect=[68]Algol,
        defaultdialect=[gnu]Awk,
        defaultdialect=[ANSI]C,
        defaultdialect=[light]Caml,
        defaultdialect=[1985]Cobol,
        defaultdialect=[WinXP]command.com,
        defaultdialect=[ISO]C++,
        defaultdialect=[95]Fortran,
        defaultdialect=[5.2]Mathematica,
        defaultdialect=[OMG]OCL,
        defaultdialect=[Standard]Pascal,
        defaultdialect=[67]Simula,
        defaultdialect=[plain]TeX,
        defaultdialect=[97]VRML}
\lstalias[]{TclTk}[tk]{tcl}
\lstalias[6.1]{ABAP}[R/3 6.10]{ABAP}
\lstalias[3.1]{ABAP}[R/3 3.1C]{ABAP}
\lstalias[4.6]{ABAP}[R/3 4.6C]{ABAP}

\input{listings-java.prf}

\endinput

%%
%% End of file `listings.cfg'.
share|improve this question
    
Welcome to TeX.SX! What TeX distribution are you running? –  egreg Jul 27 at 22:15
2  
It's not a good idea to modify a file belonging to the main tree of the distribution, because an update to the package will destroy your change. –  egreg Jul 27 at 22:20

2 Answers 2

up vote 5 down vote accepted

You shouldn't modify a file in the main distribution tree: if there is an update to the package, your modification will be lost.

In section 4.19 of the manual it is said that listings will load a file called lstlocal.cfg if it exists in a directory read by TeX. (Thanks to Paul Gessler for the hint.)

Strategy 1: create a new lstlocal.cfg in the “local tree”

  1. Create the necessary infrastructure in TEXMFLOCAL

    sudo mkdir -p $(kpsewhich -var-value TEXMFLOCAL)/tex/latex/listings
    
  2. Create lstlocal.cfg to a working directory containing

    \input{listings-java.prf}
    
  3. Move the file to the local tree

    sudo mv lstlocal.cfg $(kpsewhich -var-value TEXMFLOCAL)/tex/latex/listings
    
  4. Update the file database

    sudo mktexlsr
    

Strategy 2: create lstlocal.cfg in the “personal tree”

The same as before, but without the final step and without any sudo, but with TEXMFHOME in place of TEXMFLOCAL

  1. Create the necessary infrastructure in TEXMFHOME

    sudo mkdir -p $(kpsewhich -var-value TEXMFHOME)/tex/latex/listings
    
  2. Create lstlocal.cfg to a working directory containing

    \input{listings-java.prf}
    
  3. Move the file to the personal tree

    mv lstlocal.cfg $(kpsewhich -var-value TEXMFHOME)/tex/latex/listings
    

What are the differences in the two strategies?

With the first strategy, the listings-java.prf file will be available to all users of the machine, whereas the second strategy makes it available only to you. If you're the only user on the machine there no substantial difference. Of course, if you don't have administrator privileges, you can only follow the second strategy.

Depending on your operating system, sudo could not be available and you'll have to remove it from the commands in “Strategy 1”, running them as superuser.

If you are on Windows instead of a Unix system, translate the commands; for MiKTeX the locations may be different and doing commands as administrator will require a different method.

See Create a local texmf tree in MiKTeX for more information about the local tree on MiKTeX.

share|improve this answer
    
A note on the caveat: see section 4.19 of the listings manual. Local configurations are recommended in a file lstlocal.cfg which is loaded (if it exists) after listings.cfg. –  Paul Gessler Jul 27 at 23:11
    
@PaulGessler Thanks for the remark! It's easier, then! –  egreg Jul 27 at 23:14
    
@egreg Thank you! This worked great. I'm going to the other part of my question (regarding working with colors in this file) to a new question. –  DesAdams Jul 28 at 0:40

You have:

\endinput

\input{listings-java.prf}

in listings.cfg but \endinput tells TeX to stop reading from that file. Nothing after that will be read. So you need to move your \input command before the \endinput command:

\input{listings-java.prf}

\endinput
share|improve this answer
    
That makes sense. I made the change, but ran into new problems. See my edit. Thanks though! I'd +1 but it won't let me for lack of reputation :\ –  DesAdams Jul 27 at 22:29
    
@DesAdams Never mind. I know nothing about using listings. I just happened to notice the \endinput and figured that would explain why it really wasn't reading the file. Anyway, egreg has you in expert hands! –  cfr Jul 27 at 23:14
1  
+1 anyways (I have enough rep now ;) –  DesAdams Jul 28 at 0:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.