使用环境生成表 - “错位的对齐选项卡字符”错误

时间:2017-04-21 08:56:03

标签: latex

我正在尝试创建一组简单的环境,以使表的创建更容易和一致。 环境\observation生成空表。观察行由\subobservation命令引入。它适用于一个\subobservation命令,但我无法弄清楚如何插入多行。 使用以下代码示例,我得到“错位的对齐选项卡字符”错误。

\documentclass[11pt,a4paper]{article}
\usepackage{lipsum}

\newenvironment{observationtable}{
    \begin{center}
    \begin{tabular}{p{0.2\textwidth}p{0.6\textwidth}p{0.2\textwidth}}
    \hline\\
    }
    { 
    \end{tabular} 
    \end{center}
}

\newcommand{\subobservation}[3]{
    \textbf{#1} & #2 & \textbf{#3} \\
    \hline\\
    }

\newenvironment{observation}[1]{
    \textbf{#1}
    \begin{observationtable}
        1st Col & 2nd Col & 3rd Col \\
        \hline\\
    }{
    \end{observationtable}
    }

\begin{document}

\begin{observation}
    {An interesting collection of observation}
   \begin{subobservation}
        {some information}
        {\lipsum[1]}
        {another something}
    \end{subobservation}

    \begin{subobservation}
        {some 2nd information}
        {\lipsum[2]}
        {another 2nd something}
    \end{subobservation}

\end{observation}

\end{document}

pdfLaTeX output for described problem

1 个答案:

答案 0 :(得分:0)

我从这里得到一些灵感来解决我的问题https://tex.stackexchange.com/a/39935

\documentclass{article}

\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{lipsum}


\newbool{firstline}
\newenvironment{observation}[1]
    {%
    \subsection*{#1}
    \booltrue{firstline}%
    \begin{tabular}{p{0.15\textwidth}p{0.6\textwidth}p{0.15\textwidth}}
        \toprule\\
        \textbf{1st Col} & \textbf{2nd Col} & \textbf{3rd Col} \\ \\
        \toprule
        }
    {\\ \bottomrule\end{tabular}}

\newcommand{\subobservation}[3]{%
    \ifbool{firstline}{}{\\\midrule}%
    \global\boolfalse{firstline}\\
    #1
    &#2
    &#3
}

\begin{document}
    \begin{observation}
        {An interesting collection of observation}
        \subobservation
            {some information}
            {\lipsum[1]}
            {another something}
        \subobservation
            {some 2nd information}
            {\lipsum[2]}
            {another 2nd something}
    \end{observation}

\end{document}

enter image description here