格式化类似 SQL 的脚本

格式化类似 SQL 的脚本

假设我们有如下脚本:

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;

如何编写latex代码来实现如下效果: 在此处输入图片描述

答案1

我相信简单的verbatim环境就足够了。

\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}

输出:

在此处输入图片描述

请注意,我们必须使用左单引号`和右单引号'字符标记才能获得正确的引号。

答案2

我会把它留给listings能够理解 SQL 和许多其他语言的包。

\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,
           commentstyle=\color{gray}
        ]
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}

在此处输入图片描述

答案3

简单的方法:

\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}

在此处输入图片描述

答案4

一点打字练习:

\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}

在此处输入图片描述

相关内容