# Formatting the SQL-like script

Suppose we have a script as follows:

a = LOAD 'data' USING BinStorage AS (user);
b = GROUP a BY user;
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;


How to write the latex code to achieve the following effect:

I believe that a simple verbatim environment will suffice.

\documentclass{article}

\begin{document}
\begin{verbatim}
a = LOAD data' USING BinStorage AS (user);
b = GROUP a BY user;
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;
\end{verbatim}
\end{document}


Output:

Notice that we have to use the left single quote  and right single quote ' characters marks to get the right quotation marks.

• The text flows out of the page, how do we wrap text here? – Yogeesh Seralathan Nov 21 '17 at 0:10

I would leave it to listings package which understands SQL among many many other languages.

\documentclass{article}
\usepackage{xcolor,listings}
\usepackage{textcomp}
\lstset{upquote=true}

\begin{document}
Here is an example
\begin{lstlisting}[
language=SQL,
showspaces=false,
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
]
a = LOAD 'data' USING BinStorage AS (user);
b = GROUP a BY user;
/* Now we are ready to loop */
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;
\end{lstlisting}

\end{document}


• Does listing provide a way to "correct" the quote. – Aditya Nov 26 '13 at 3:09
• @Aditya Now fixed – percusse Nov 26 '13 at 3:12

Two easy ways:

\documentclass{article}
\begin{document}

For example:

{\obeylines\obeyspaces
\texttt{
a = LOAD 'data' USING BinStorage AS (user);
b = GROUP a BY user;
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;
}}

\medskip
Or:

\begin{verbatim}
a = LOAD 'data' USING BinStorage AS (user);
b = GROUP a BY user;
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;
\end{verbatim}

\end{document}


• I can't remember -- does \texttt take \par tokens? It might be better in this case to use \ttfamily. – Sean Allred Nov 26 '13 at 3:30

Little typing practice:

\documentclass{article}
%% \usepackage{listings} you can use this also see the manual
%\usepackage[scaled=.9]{beramono}
\begin{document}
Another case where Pig is able to know the type of a field even when the program has not declared types is when operators or user-defined functions (UDFs) have been applied whose return type is known. In the following example Pig will order the output data numerically since it knows that the return type of \texttt{COUNT} is \verb|long|.
\begin{verbatim}
a = LOAD data' USING BinStorage AS (user);
b = GROUP a BY user;
c = FOREACH b GENERATE COUNT(a) AS cnt;
d = ORDER c BY cnt;
\end{verbatim}

\end{document}


• You just beat me to it! I should add that you need to use the left single quote  and right single quote ' characters to get the quotes "right". – I Like to Code Nov 26 '13 at 2:27
• @ILiketoCode I simply copy pasted :-) – user11232 Nov 26 '13 at 2:35
• Obligatory rant whenever I see wrong quotes typeset by TeX. Please use a unicode engine (xetex or luatex), load the fontspec package and a unicode teletype font. – Aditya Nov 26 '13 at 3:04