2

When I write truth tables I like a bit more space in the cells, so I assign new values to arraystretch and tabcolsep, like this ...

\documentclass[12pt]{article}
\begin{document}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm}
\begin{tabular}{| c  | c | c | c | c |}
    \hline
    $Q$ & $R$ & $Q \lor R$ \\ \hline \hline
    T & T & T \\
    T & F & T \\
    F & T & T \\
    F & F & F \\
    \hline
\end{tabular}
\end{document}

... and all is well.

Eventually I made a new environment for truth tables:

\newenvironment{truthtabular}[1]{%
    \renewcommand{\arraystretch}{1.5}
    \renewcommand{\tabcolsep}{0.2cm}
    \begin{tabular}{#1}{%
    }
}{\end{tabular}}

The environment worked fine unless I wanted the hline at the top, in which case I would get the following errors:

Misplaced \noalign. \hline
You can't use `\hrule' here except with leaders. \hline
Missing number, treated as zero. \hline
Illegal unit of measure (pt inserted). \hline

Attempting to diagnose the problem, I removed the reassignments from the definition of the new environment, thus:

\newenvironment{truthtabular}[1]{%
    %\renewcommand{\arraystretch}{1.5}
    %\renewcommand{\tabcolsep}{0.2cm}
    \begin{tabular}{#1}{%
    }
}{\end{tabular}}

The compilations still fails, with the same errors, when beginning with an hline. Example of failure follows:

\documentclass[12pt]{article}
\begin{document}

\newenvironment{truthtabular}[1]{%
    \begin{tabular}{#1}{%
    }
}{\end{tabular}}

\begin{truthtabular}{| c  | c | c | c | c |}
    \hline
    $Q$ & $R$ & $Q \lor R$ \\ \hline \hline
    T & T & T \\
    T & F & T \\
    F & T & T \\
    F & F & F \\
    \hline
\end{truthtabular}
\end{document}

Why does the leading hline cause problems in the defined environment? What can be done to avert the problems?

2

You have a spurious pair of braces in your definition.

I slightly improved your table, using \\hhline for the double line, and I propose another layout:

 \documentclass[12pt]{article}
\usepackage{array, hhline, booktabs} 
\usepackage[x11names, table]{xcolor} 
\newenvironment{truthtabular}[1]{%
    \renewcommand{\arraystretch}{1.5}
    \setlength{\tabcolsep}{0.2cm} \tabular{#1}
}{\endtabular}

\begin{document}

\begin{truthtabular}{| c | c | c | }
    \hline
    $Q$ & $R$ & $Q \lor R$ \\ %
    \hhline{:=:=:=:}
    T & T & T \\
    T & F & T \\
    F & T & T \\
    F & F & F \\
    \hline
\end{truthtabular}

{ % begin box
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\arrayrulecolor{LightSteelBlue3}
 \begin{truthtabular}{cc!{\color{LightSteelBlue3}\vrule width1.5pt}c }
 $Q$ & $R$ & $Q \lor R$ \\ %
 \midrule
 T & T & T \\
 T & F & T \\
 F & T & T \\
 F & F & F 
\end{truthtabular}
} % end box

\end{document} 

enter image description here

  • Yes, that was it. (First new environment for me.) Thank you very much! – Homer White Sep 23 '16 at 2:24
  • You're welcome! I've added another, nicer in my opinion, way of displaying the table. – Bernard Sep 23 '16 at 2:44
  • Excellent! And helpful in for me in getting started on hhline and booktab. – Homer White Sep 23 '16 at 11:53
  • Concerning booktabs, note I made an unorthodox use of it: normally it adds some vertical padding around horizontal rules (\above/belowrulesep), which I had to annihilate to have crossing vertical and horizontal rules. Normally, you shouldn't use vertical rules with booktabs. – Bernard Sep 23 '16 at 12:40
3

The \hrule has to be the first element in the row. In your example, you've placed an empty group at the top of the truthtable environment since you finished off your \begin{truthtable} definition with an empty group. The following minimal example replicates your problem:

\documentclass{article}

\begin{document}

\begin{tabular}{c}
  {} \hline % <---- not good
  table
\end{tabular}

\end{document}

Remove this empty group (as well as the spurious spaces) all peace will be restored:

enter image description here

\documentclass{article}

\usepackage{booktabs,eqparbox}

\newenvironment{truthtabular}[1]
  {\renewcommand{\arraystretch}{1.5}% https://tex.stackexchange.com/a/31704/5764
   %\setlength{\tabcolsep}{0.2cm}% ...if you still need it
   \begin{tabular}{#1}}
  {\end{tabular}}

\begin{document}

\begin{truthtabular}{ *{3}{c} }
  \toprule
  \eqmakebox[ttt]{$Q$} & \eqmakebox[ttt]{$R$} & \eqmakebox[ttt]{$Q \lor R$} \\
  \midrule
  T & T & T \\
  T & F & T \\
  F & T & T \\
  F & F & F \\
  \bottomrule
\end{truthtabular}

\end{document}

I've used booktabs for presentation and spacing equality via eqparbox.

  • (+1) Nice idea to use eqparbox! – Bernard Sep 23 '16 at 9:41
  • Yes, it looks very nice! – Homer White Sep 23 '16 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.