使用 lstlisting 时表格上的边框位置错误

使用 lstlisting 时表格上的边框位置错误

我在使用环境时遇到了一个问题tabular。以下代码

\documentclass [12pt,a4paper,oneside]{report}
\usepackage {listings}
\usepackage {fontspec}
\usepackage {enumitem} 
\usepackage {parskip}
\usepackage {setspace} 

\lstnewenvironment{sqlcode}[2][]%
{%
  %\minipage{\textwidth} 
  \lstset{
    basicstyle={\footnotesize\ttfamily\singlespacing},
    language=SQL,
    breaklines=true,
    breakatwhitespace=true, 
    captionpos=none,
    numbers=none,
    frame=none,
    tabsize=2,
    extendedchars=true,
    caption=#1,
    label=#2
  }
}
{  
  %\endminipage
}


\begin {document}

\begin {center}
  \begin {table}
    \begin {tabular}{| l | l | l | p{5cm} |}
    \hline
    \textbf {Table Name} & \multicolumn {3}{c |}{  } \\ \hline
    Attribute & Description & Type & Example \\ \hline
    SQL Code & \multicolumn {3}{ l |}{ }
    {
      \begin{sqlcode}[SQL Code for Project table]{sqlproject}
      CREATE TABLE project
      (
        projectid bigint NOT NULL,
        name character varying(255),
        description character varying(255),
        address character varying(255),
        projectpriority character varying(255),
        CONSTRAINT project_pkey PRIMARY KEY (projectid)
      )
      WITH (
        OIDS=FALSE
      );
      \end{sqlcode} 
    } \tabularnewline \hline
  \end {tabular} 
  \caption {Table Properties}
  \end {table}

\end {center}

\end {document}

生成以下输出:

错误的表格边框

问题是表格边框在第三行显示不正确。我是不是漏掉了什么?

答案1

问题与你使用的有关\multicolumn,它需要三个参数。第一个参数给出列的跨度,第二个参数为合并的列提供更新的列规范,最后一个参数提供内容。你提供了“四个”。即使你使用,\multicolumn{3}{l|}{<sqlcode>}你仍然会遇到问题,因为你传递了逐字-like 内容到宏...这在 LaTeX 中不容易做到。您可以通过在将内容传递给之前对其进行装箱来规避这种情况\multicolumn

在此处输入图片描述

\documentclass [12pt,a4paper,oneside]{report}
\usepackage{listings}% http://ctan.org/pkg/listings

\lstnewenvironment{sqlcode}[2][]%
{%
  %\minipage{\textwidth} 
  \lstset{
    basicstyle={\footnotesize\ttfamily\singlespacing},
    language=SQL,
    breaklines=true,
    breakatwhitespace=true, 
    captionpos=none,
    numbers=none,
    frame=none,
    tabsize=2,
    extendedchars=true,
    caption=#1,
    label=#2
  }%
}
{%
  %\endminipage
}
\newsavebox{\sqlbox}

\begin {document}

\begin{lrbox}{\sqlbox}
\begin{sqlcode}[SQL Code for Project table]{sqlproject}
CREATE TABLE project
(
  projectid bigint NOT NULL,
  name character varying(255),
  description character varying(255),
  address character varying(255),
  projectpriority character varying(255),
  CONSTRAINT project_pkey PRIMARY KEY (projectid)
)
WITH (
  OIDS=FALSE
);
\end{sqlcode} 
\end{lrbox}

\begin {table}
  \begin {tabular}{| l | l | l | p{5cm} |}
    \hline
    \textbf {Table Name} & \multicolumn {3}{c |}{  } \\ \hline
    Attribute & Description & Type & Example \\ \hline
    SQL Code & \multicolumn {3}{ l |}{\raisebox{0pt}[\dimexpr\height-\normalbaselineskip]{\usebox{\sqlbox}}} \\ \hline
  \end {tabular} 
  \caption {Table Properties}
\end {table}

\end {document}

代码还做了一些其他更改。

相关内容