我正在尝试使用该包格式化一些 SQL 语句listings
。对于小语句,它看起来还不错,但对于长语句,它看起来很糟糕。
select * from Customers where C_nID=10;
到
select *
from customers
where C_nID=10;
所以我尝试创造自己的风格
这有效:
\lstdefinelanguage{mojsql}
{keywords={SYSDATETIME,sysdatetime,the,list,is,too,long%
sensitive=true,%
comment=[l]{\#},%
literate= {ä}{{a}}1 {\\}{{}}0 {?}{{\textcolor{red}{\textbf{?}}}}1,
string=[b]",%
string=[b]'%
}
\lstset{
language=mojsql, %% Troque para PHP, C, Java, etc... bash é o padrão
extendedchars=true,
breaklines=true, breakatwhitespace=false,
numbers=left,
frame=single
}
然后我想格式化语句(例如,在每个语句之前添加一个新行where
等等)。我找到了\lstdefineformat{}
命令,但它不起作用(命令未定义错误)。我也尝试了\usepackage[format]{listings}
导入,但出现了选项冲突包listings
错误
那么我如何确保在每个where
关键字或其他关键字前插入一个新行?
更新:
好的,我找到了问题所在,我可以执行该功能,但不幸的是又出现了新的问题。我在工作示例中描述了它们。
\documentclass[12pt,oneside,a4paper]{book}
\usepackage[formats]{listings}
\usepackage{color}
\lstdefinelanguage{mojsql}
{keywords={select,top,from,too,long,for,a,minimal,example},%
sensitive=false,%
morecomment=[s]{/*}{*/},%
morecomment=[l]{//},%
literate= {ä}{{a}}1 {\\}{{}}0 {?}{{\textcolor{red}{\textbf{?}}}}1 {1}{{\textcolor{red}{\textbf{1}}}}1,%I tried something like {AND}{\newline AND}3 the text was repleaced bud the new line command was ignored ...
string=[b]",%
string=[b]'%
}
%The command works now (I had a typo and an older version of listings Im not sure which one fixed the problem)
\lstdefineformat{mojsqlFormat}{
\{ =\newline\string,%this works fine ( to test replace "{" with "(" there are no { in the statement
%I want the exact same thing with the WHERE keyword but where=\newline\string does not work -> ! LaTeX Error: Missing \begin{document}. Is there simple solution for this?
%,where=\newline\string
}
\lstset{
language=mojsql, % Troque para PHP, C, Java, etc... bash é o padrão
format=mojsqlFormat,
extendedchars=true,
breaklines=true, breakatwhitespace=false,
numbers=left,
frame=single
}
\begin{document}
%long unformated sql statement (im a script to inject it before the compilation)
\begin{lstlisting}
SELECT (SELECT TOP 1 a.id FROM vAnalysesHistory AS a WHERE a.companyid = n.stockid ORDER BY a.chosendatetime DESC) AS id, n.name, (SELECT TOP 1 a.chosendatetime FROM vAnalysesHistory AS a WHERE a.companyid = n.stockid ORDER BY a.chosendatetime DESC) AS chosendatetime FROM vStockNames AS n
\end{lstlisting}
\end{document}
答案1
使用\ WHERE=\newline\string
(注意反斜杠后面的空格)似乎有效。这是前:
和后:
\lstdefineformat{mojsqlFormat}{
\{ =\newline\string,%this works fine ( to test replace "{" with "(" there are no { in the statement
%I want the exact same thing with the WHERE keyword but where=\newline\string does not work -> ! LaTeX Error: Missing \begin{document}. Is there simple solution for this?
,\ WHERE=\newline\string
}