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.

I've just come accross a problem I'm unable to solve.

I use this code in chapter 2 and it all works fine :

\begin{ttfamily}
\begin{lstlisting}
<?php
$template = 'blue.php';
    if ( is_set( $_COOKIE['TEMPLATE'] ) )
    $template = $_COOKIE['TEMPLATE'];
include("/home/users/phpguru/templates/". $template );
?>
\end{lstlisting}
\end{ttfamily}

Then I actually use the same code in chapter 4, but surprisingly I get a boxed (or framed) result:

\begin{ttfamily}
\begin{lstlisting}
// connect to the database
$conn = mysql_connect("localhost","username","password");
// dynamically build the sql statement with the input
$query = "SELECT userid FROM CMSUsers WHERE user = '$_GET["user"]' " .
"AND password = '$_GET["password"]'";
// execute the query against the database
$result = mysql_query($query);
// check to see how many rows were returned from the database
$rowcount = mysql_num_rows($result);
// if a row is returned then the credentials must be valid, so
// forward the user to the admin pages
if ($rowcount != 0){ header("Location: admin.php");}
// if a row is not returned then the credentials must be invalid
else { die('Incorrect username or password, please try again.')}
\end{lstlisting}
\end{ttfamily}

I must say that in chapter 3 (so different location but before the strange behaviour occured in chapter 4, so to speak) I use this code in order to get a framed code snippet:

\lstset{framexleftmargin=0mm, frame=shadowbox, rulesepcolor=\color{black},basicstyle=\ttfamily\small}
\begin{figure} [H]
\begin{lstlisting}
root@bt:~# echo -en "GET/HTTP/1.0\n\n\n"|nc www.we****t***.com 80\
>|grep Server
Server: Apache/2.2.15
\end{lstlisting}
\caption{Identificazione server}
\end{figure}

What should I do? I tried to delete all files not having .tex extension (aux,toc,bak..etc), but to no avail.

share|improve this question
2  
You've changed the listings settings in Chapter 3, and assuming the obvious logical order, these settings will apply in Chapter 4 (etc.). You'll need to either localise the settings or reset them at the start of Chapter 4. –  Joseph Wright Jan 5 '13 at 9:12
    
What should I do exactly in chapter 3?...I believe the problem is here : \lstset{framexleftmargin=0mm, frame=shadowbox, rulesepcolor=\color{black},basicstyle=\ttfamily\small} Am I right? But I don't know what to do.. –  g9999 Jan 5 '13 at 9:16
2  
A full minimal working example (MWE) would be useful here. What I'm really not clear on is whether the change in Chapter 3 applies to several items or just one or two: if it's the latter, the best approach will be to localise the change. –  Joseph Wright Jan 5 '13 at 9:27
6  
When you use \lstset it applies those changes to all lstlisting environments that follow, so you're seeing the expected behaviour. To apply settings to a single lstlisting, use the optional argument to the environment: \begin{lstlisting}[frame=shadowbox, <any other settings you want>] –  Torbjørn T. Jan 5 '13 at 9:37
1  
Could you please try to rewrite your heading to be more informative? It would also be greatly appreciated if you looked back into some of your older questions and accepted an answer if you feel there is one which solves your problem. –  Vivi Jan 5 '13 at 10:46
show 2 more comments

1 Answer

As @Torbjørn says the \lstset command has a global scope, so it affects every listing after it. That's why your listing in Chapter 4 gets framed. If you only want one listing to be framed, use the optional argument to the environment:

\begin{lstlisting}[framexleftmargin=0mm, frame=shadowbox, rulesepcolor=\color{black}, basicstyle=\ttfamily\small]

You should also use \lstset{basicstyle=\ttfamily} already in Chapter 3 instead of \begin{ttfamily}.

share|improve this answer
    
an alternative might be just to move the \lstset code inside the figure environment; unless the listings package defines \lstset as global, the alteration should go away at the end of the figure. –  barbara beeton Jan 5 '13 at 14:37
add comment

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.