GeneralDisplay source code in latex

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Dooley
Posts: 3
Joined: Fri Apr 11, 2008 8:06 pm

Display source code in latex

Post by Dooley »

Hey all,

I have a few questions on inserting source code into a latex document.
I'm using the package listings, but its not going great.

Firstly I'm having a basic problem, the code I'm using doesn't go onto a new line,
so all of my code gets cut off.

Here's how I set the system to implement SQL.

Code: Select all

\lstset{language=SQL,frame=ltrb,framesep=5pt,basicstyle=\normalsize,
  keywordstyle=\ttfamily\color{OliveGreen},
 identifierstyle=\ttfamily\color{CadetBlue}\bfseries, 
 commentstyle=\color{Brown},
 stringstyle=\ttfamily,
 showstringspaces=ture}
And here is where I display some source code.

Code: Select all

\begin{lstlisting}
 "SELECT username FROM members WHERE username = '$inputuser' AND user_password = '$input_password' LIMIT 1;"
\end{lstlisting} 
For some reason this is also putting in an odd character where there should be spaces.

My second question is does anyone know if its possible to use multiple languages in one document. I would like to use both PHP and MySQL.

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Display source code in latex

Post by localghost »

Add a corresponding option in the parameter list for your listings.

Code: Select all

\lstset{
  breaklines=true,                                     % line wrapping on
  language=SQL,
  frame=ltrb,
  framesep=5pt,
  basicstyle=\normalsize,
  keywordstyle=\ttfamily\color{OliveGreen},
  identifierstyle=\ttfamily\color{CadetBlue}\bfseries,
  commentstyle=\color{Brown},
  stringstyle=\ttfamily,
  showstringspaces=ture
}
You can also give that parameter as an optional argument to the lstlisting environment as local setting.

Code: Select all

\begin{lstlisting}[breaklines=true]
  "SELECT username FROM members WHERE username = '$inputuser' AND user_password = '$input_password' LIMIT 1;"
\end{lstlisting}
Now you only have to choose how you use that.


MfG
Thorsten¹
Post Reply