如何使用 listing 包很好地排版这个 MySql 查询?

如何使用 listing 包很好地排版这个 MySql 查询?

我正在尝试在我的 LaTeX 文档中显示 MySql explain 的输出。我正在使用listings包来制作列表。
示例 LaTeX 代码:

\begin{lstlisting}[language=SQL,caption={SQL query explained}]
mysql> explain 
    select dd.data from dane dd 
      join test1.tag1 t1 on dd.id = t1.id 
      join test1.tag2 t2 on dd.id = t2.id 
      join test1.tag3 t3 on dd.id = t3.id 
      join test1.tag4 t4 on dd.id = t4.id 
      join test1.tag5 t5 on dd.id = t5.id 
      join test1.tag6 t6 on dd.id = t6.id 
      join test1.tag7 t7 on dd.id = t7.id 
      join test1.tag8 t8 on dd.id = t8.id 
      join test1.tag9 t9 on dd.id = t9.id 
      join test1.tag10 t10 on dd.id = t10.id 
    where t1.val = 1 
      and t2.val = 2 
      and t3.val = 3 
      and t4.val = 4 
      and t5.val = 5 
      and t6.val = 6 
      and t7.val = 7 
      and t8.val = 8 
      and t9.val = 9 
      and t10.val = 10 ;
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref               | rows  | Extra                                                         |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
|  1 | SIMPLE      | t1    | ALL    | PRIMARY       | NULL    | NULL    | NULL              | 99901 | Parent of 10 pushed join@1; Using where with pushed condition |
|  1 | SIMPLE      | t2    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t1.id,const |     1 | Child of 't1' in pushed join@1                                |
|  1 | SIMPLE      | t3    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t2.id,const |     1 | Child of 't2' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t4    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t3.id,const |     1 | Child of 't3' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t5    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t4.id,const |     1 | Child of 't4' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t6    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t1.id,const |     1 | Child of 't5' in pushed join@1                                |
|  1 | SIMPLE      | t7    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t5.id,const |     1 | Child of 't6' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t8    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t7.id,const |     1 | Child of 't7' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t9    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t5.id,const |     1 | Child of 't8' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t10   | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t8.id,const |     1 | Child of 't9' in pushed join@1; Using where                   |
|  1 | SIMPLE      | dd    | eq_ref | PRIMARY       | PRIMARY | 4       | test1.t5.id       |     1 | Using where                                                   |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
11 rows in set (0.01 sec)
\end{lstlisting}

pdf 结果如下: pdf 结果

有两个问题:
1. 文本太宽,不适合页面。2
. 在 LaTeX 源文件中,空格用于排列数据列。在 pdf 输出中,空格被截断。

我将非常感激任何关于如何处理这些问题并将解释输出转换为可查看的 pdf 格式的想法。

答案1

由于此列表特别广泛,您应该

  • 例如,通过传递键\tiny来设置小字体大小basicstyle
  • landscape例如,通过使用包提供的环境以横向显示列表pdflscape

此外,为了确保尊重空间(粗略地说),你应该

  • \ttfamily通过传递给键切换到等宽字体(而不是默认的比例字体)basicstyle
  • 使用该columns=fixed选项(有关详细信息,请参阅listings手册中的 2.10)。

但是,如果您使用 Computer Modern 或 Latin Modern 字体,您的SQL关键字将不会以粗体形式排版,因为这些字体不包含粗体系列的打字机字体。为了解决这个问题,您可以随时切换到一些字体,例如 Courier(如下所示)或 Bera Mono。

在此处输入图片描述

\documentclass{article}

\usepackage{lipsum} % for filler text
\usepackage{listings}
\usepackage{courier}
\usepackage{pdflscape}

\lstdefinestyle{SQL-Michalstyle}
{%
  language   = SQL,
  caption    = {SQL query explained},
  basicstyle = \tiny\ttfamily,
  columns    = fixed,
}

\lstset{style=SQL-Michalstyle}

\begin{document}

\lipsum

\begin{landscape}
\begin{lstlisting}
mysql> explain 
    select dd.data from dane dd 
      join test1.tag1 t1 on dd.id = t1.id 
      join test1.tag2 t2 on dd.id = t2.id 
      join test1.tag3 t3 on dd.id = t3.id 
      join test1.tag4 t4 on dd.id = t4.id 
      join test1.tag5 t5 on dd.id = t5.id 
      join test1.tag6 t6 on dd.id = t6.id 
      join test1.tag7 t7 on dd.id = t7.id 
      join test1.tag8 t8 on dd.id = t8.id 
      join test1.tag9 t9 on dd.id = t9.id 
      join test1.tag10 t10 on dd.id = t10.id 
    where t1.val = 1 
      and t2.val = 2 
      and t3.val = 3 
      and t4.val = 4 
      and t5.val = 5 
      and t6.val = 6 
      and t7.val = 7 
      and t8.val = 8 
      and t9.val = 9 
      and t10.val = 10 ;
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref               | rows  | Extra                                                         |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
|  1 | SIMPLE      | t1    | ALL    | PRIMARY       | NULL    | NULL    | NULL              | 99901 | Parent of 10 pushed join@1; Using where with pushed condition |
|  1 | SIMPLE      | t2    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t1.id,const |     1 | Child of 't1' in pushed join@1                                |
|  1 | SIMPLE      | t3    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t2.id,const |     1 | Child of 't2' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t4    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t3.id,const |     1 | Child of 't3' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t5    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t4.id,const |     1 | Child of 't4' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t6    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t1.id,const |     1 | Child of 't5' in pushed join@1                                |
|  1 | SIMPLE      | t7    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t5.id,const |     1 | Child of 't6' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t8    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t7.id,const |     1 | Child of 't7' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t9    | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t5.id,const |     1 | Child of 't8' in pushed join@1; Using where                   |
|  1 | SIMPLE      | t10   | eq_ref | PRIMARY       | PRIMARY | 8       | test1.t8.id,const |     1 | Child of 't9' in pushed join@1; Using where                   |
|  1 | SIMPLE      | dd    | eq_ref | PRIMARY       | PRIMARY | 4       | test1.t5.id       |     1 | Using where                                                   |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+-------+---------------------------------------------------------------+
11 rows in set (0.01 sec)
\end{lstlisting}
\end{landscape}

\lipsum

\end{document}

相关内容