有条件地发出一个表或另一个表

有条件地发出一个表或另一个表

我想指定一个表格,如果它有多行单元格,则应将其输出为一对列,否则应将其输出为两对列。例如,如果它看起来像下面代码中的第一个表格,则应按原样输出。如果它看起来像下面代码中的第二个表格,则应将其输出为第三个表格。

首先,我该如何实现这一点?我尝试将代码放入\sbox但一直出现错误;但是,即使我这样做了,我也不知道下一步该怎么做。添加请注意,由于我是从程序生成该表格的,因此可以在乳胶代码中两次指定表中的数据。

其次,下面的代码会产生大量消息,例如(\end occurred when \ifhmode on line 35 was incomplete)虽然它仍在运行。这些消息是什么意思?我该如何处理它们?

\documentclass{article}
\usepackage[top=1in,bottom=1in,left=0.5in,textwidth=7.5in]{geometry}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{tabu}
\usepackage{xcolor}
\begin{document}

First table.

\setlength{\columnseprule}{.4pt} 
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
\end{tabu} 
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{multicols} 

Second table.

\setlength{\columnseprule}{.4pt} 
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
\end{tabu} 
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words &  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu- lum ut, placerat ac, adipiscing vitae, felis. \\
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{multicols} 

Third table.

\begin{center}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words &  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu- lum ut, placerat ac, adipiscing vitae, felis. \\
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{center}

\end{document}

结果是:

在此处输入图片描述

添加

请注意,这些帖子之间存在一些关系:

隐藏整个表格(但仍生成它)

多列中的垂直对齐

答案1

我添加了额外的打印内容以帮助解释该方法。并在默认文章中完成(抱歉,没有,geometry但这不是障碍)。由于您提到数据是自动生成的,我要求您将它写入名为 tabledata.tex 的外部文件,对于此 MWE,该文件的一个例子是:

Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & more / words
Some words & and some more

我使用readarray包通过 读取数据\readrecordarray。由于文件末尾有一个空白行,因此我从 ROWS 中减去了一个,但如果您可以在没有多余行的情况下写入文件,则可以跳过该部分。

然后我遍历每一行数据并将其设置在 tabu 中并测量其宽度,以确定它是窄还是宽。我将其用作300pt阈值,但您可以对其进行调整。如果表中的任何行超出该长度,则该表被视为宽。

\if一旦判断出桌子是宽还是窄,接下来就是使用树枝来布置合适桌子的问题了。

\documentclass{article}
\parindent 0in
\usepackage{tabu}
\usepackage{readarray}
\usepackage{ifthen}
\usepackage[nomessages]{fp}
\usepackage{xcolor}
\usepackage{multicol}
\global\newlength\boxwidth
\newcommand\showwidth[1]{\findwidth{#1}\the\boxwidth}
\newcommand\findwidth[1]{\setbox0\hbox{#1}\setlength\boxwidth{\wd0}}
\newcounter{index}
\begin{document}
\readrecordarray{tabledata.tex}\mydata
\setcounter{index}{\mydataROWS}
\addtocounter{index}{-1}
\edef\mydataROWS{\arabic{index}}
Data rows: \mydataROWS

Here are the measure of the data rows

\setcounter{index}{0}\def\widetable{F}
\whiledo{\value{index} < \mydataROWS}{%
  \addtocounter{index}{1}%
  \def\y{%
    \begin{tabu} to%
      \columnwidth{>{\bfseries}l>{\strut\ignorespaces}l<{\ifhmode\unskip\strut\fi}}%
      \everyrow{\tabucline[blue]-}%
      \bfseries\mydata[\arabic{index}]\\%
    \end{tabu}%
  }%END DEF
  \showwidth{\y}~~\y\\%
  \findwidth{\y}
  \ifthenelse{\lengthtest{\boxwidth > 300pt}}{\def\widetable{T}}{}
}%END WHILEDO

The table is wide? \widetable

\if T\widetable
  \begin{center}
  \tabulinesep=4pt
  \noindent
  \setcounter{index}{0}
  \begin{tabu} %
    to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
    \everyrow{\tabucline[blue]-}
    \whiledo{\value{index} < \mydataROWS}{%
      \addtocounter{index}{1}%
      \mydata[\arabic{index}]%
      \ifthenelse{\equal{\arabic{index}}{\mydataROWS}}{}{\\}%
    }
  \end{tabu} 
  \end{center}
\else
  \setlength{\columnseprule}{.4pt} 
  \renewcommand\columnseprulecolor{\color{blue}}
  \FPeval{\dblrows}{trunc((\mydataROWS+1)/2,0)}
  Rows for double-column table: \dblrows
%  
  \renewcommand\columnseprulecolor{\color{blue}}
  \begin{multicols}{2}
    \tabulinesep=4pt
    \noindent
    \setcounter{index}{0}
    \begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
      \everyrow{\tabucline[blue]-}
      \whiledo{\value{index} < \dblrows}{%
        \addtocounter{index}{1}
        \mydata[\arabic{index}]%
        \ifthenelse{\equal{\arabic{index}}{\dblrows}}{}{\\}%
      }
    \end{tabu} 
    \tabulinesep=4pt
    \noindent
    \begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
      \everyrow{\tabucline[blue]-}
      \whiledo{\value{index} < \mydataROWS}{%
        \addtocounter{index}{1}
        \mydata[\arabic{index}]%
        \ifthenelse{\equal{\arabic{index}}{\mydataROWS}}{}{\\}%
      }
    \end{tabu} 
  \end{multicols} 
\fi
\end{document}

对于宽数据示例:

在此处输入图片描述

对于窄数据示例:

在此处输入图片描述

附言:对于您的问题“这些消息是什么意思?我该如何处理它们?”,每个消息都\if必须以 结尾\fi。您的消息\ifhmode没有被关闭,因此造成了麻烦。

相关内容