自定义环境中的表问题

自定义环境中的表问题

我有一个大型文档,其中我使用并重复使用数据片段。这样的数据片段由数字、简短和详细描述以及值组成。例如。我想提前声明所有这些数据片段,如下所示:

\begin{datasnippets}
\datasnippet{licenseplate}{A licenseplate}{A valid licenseplate for Belgium}{1BEL500} 
\datasnippet{building}{An expensive house}{The address of a building costing more than 1 million}{1 Google Drive CA} 
\end{datasnippets}

然后我希望这个环境(a)生成一个包含所有数据片段的表,以及(b)生成一组命令,允许我引用这些片段的不同部分。

因此,上面的代码片段后面跟着以下句子:

I found a license plate (\licenseplateValue ). The car was located at \buildingValue . 

看起来会像这样。

在此处输入图片描述

目前我实现如下:

\newcounter{datasnippets}
\newcommand{\thedatasnippetcounter}{\arabic{datasnippets}}
\newenvironment{datasnippets}
{
    \small\\
    \hspace{-2cm}\begin{longtable}{p{0.4cm} p{4cm}p{3cm}p{10cm} }
  \ & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
    \ & \ & \ & \\
    \hline
    \newcommand{\datasnippet}[4]{%
        \refstepcounter{datasnippets}
        \label{data:##1}
        \global\expandafter\def\csname ##1\endcsname{##2}
        \global\expandafter\def\csname ##1Description\endcsname{##3}%
        \global\expandafter\def\csname ##1Value\endcsname{##4}%     
      \global\expandafter\def\csname ##1Full\endcsname{##3 (waarde: ##4)}%      

        \hspace{-2cm}
        \begin{tabular}{p{0.2cm}p{4cm}p{3cm}p{10cm} }
        \thedatasnippetcounter . & ##2 & ##4 & ##3 \\
        \ & \ & \ & \\
        \end{tabular}
    }
}
{
    \end{longtable}
}

结果在某种程度上符合我的要求,但是表格太长时表现非常糟糕,因为它不会跨页。另外,我在表格中使用表格是一个穷人的解决方案,因为如果没有内部表格,我就无法让它工作(我不知道为什么——外部表格是打开的,只在最后关闭?)。

所以我的问题是:如何在没有第二个表格的情况下使其工作,以及如何让表格很好地分布在多个页面上?

谢谢,

答案1

目前还不清楚内部tabular是做什么用的:

\documentclass{article}

\usepackage[
  left=1cm,
  right=1cm,
  textheight=3cm, % just to make the example smaller
]{geometry}

\usepackage{longtable}

\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{#4}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: #4)}%
  \thedatasnippets. & #2 & #4 & #3 \\
}
\newenvironment{datasnippets}
 {\par
  \small
  \begin{longtable}{p{0.4cm}p{4cm}p{3cm}p{10cm}}
  & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
  \hline
  \endhead
 }
 {%
  \end{longtable}
 }

\begin{document}

\begin{datasnippets}
\datasnippet{licenseplate}
  {A licenseplate}
  {A valid licenseplate for Belgium}
  {1BEL500}
\datasnippet{building}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building2}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building3}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA}
\datasnippet{building4}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building5}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building6}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building7}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\end{datasnippets}

I found a license plate (\licenseplateValue). The car was located at \buildingValue. 

\end{document}

在此处输入图片描述

当然,您需要设置长度以免溢出文本宽度。

这是一个不同的实现:

\documentclass{article}

\usepackage{longtable,array}

\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{#4}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: #4)}%
  \thedatasnippets. & #2 & #4 & #3 \\
}
\newenvironment{datasnippets}
 {\par
  \small
  \begin{longtable}{
    p{0.4cm}
    p{4cm}
    p{3cm}
    >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
  }
  & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
  \hline
  \endhead
 }
 {%
  \end{longtable}
 }

\begin{document}

\begin{datasnippets}
\datasnippet{licenseplate}
  {A licenseplate}
  {A valid licenseplate for Belgium}
  {1BEL500}
\datasnippet{building}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building2}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building3}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA}
\datasnippet{building4}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building5}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building6}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building7}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\end{datasnippets}

I found a license plate (\licenseplateValue). The car was located at \buildingValue. 

\end{document}

在此处输入图片描述

相关内容