TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to typeset something like the following picture.

enter image description here

I came up with

\documentclass[oneside,12pt]{article}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\begin{document}
\[
  \begin{aligned}
    &\neg&(P &\wedge&R)\\
    &T&&F&\\
  \end{aligned}
\]
\end{document}

How can I typeset a table like the above one?

share|improve this question
    
Do you have to do lots of these? Are you teaching this or learning it? – cfr Jan 10 at 3:58
    
I have some examples of that table on a logic textbook. And, I'm learning to typeset solutions to textbook problems. – crocket Jan 10 at 4:00
    
this answer is about generating (automatically computed) truth tables with 0 and 1's. Going to T and F isn't a problem, but what is more serious is that you are assigning truth values to sub-expressions. A modification of the \xintexpr parser, dealing with sequences of T's` and F's could do it, but would be a bit of overkill, as it would allow permissive P 'and' Q 'or' 'not' R 'and' S or parentheses arising from expansion. With a stricter input syntax such as (P and Q) or (not(R) and S) it is much easier, (continued) – jfbu Jan 11 at 9:40
    
and with input such as (\P\AND\Q)\OR(\NOT(\R)\AND\S) then it would be almost a breeze. (especially if we do not insist on treating it completely expandably). – jfbu Jan 11 at 9:41
up vote 6 down vote accepted

Logic is not all that well supported in LaTeX, unfortunately. At least, if it is, I haven't managed to track down the support. Everything always seems to involve a solution intended for some other problem and making it do something else.

If you have the patience, you could use something like this. I find tabular less hassle than a maths environment because all the Ts and Fs and Ps and Qs and so on need to be in what is essentially a text font anyway. (At least if your text font is also used for operators and so on in maths mode, which is usual.) And switching into maths mode for other things is less of a pain than typing \text{} all the time for text mode.

\documentclass[oneside,12pt]{article}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\begin{document}
  \begin{tabular}{*{4}{c}|r@{}ccc@{\thinspace}r@{}*{3}{c}@{\thinspace}c@{}lcr@{}*{3}{c}@{\thinspace}c@{\thinspace}c@{}l}
  P&Q&R&S&$(\{$&S&$\land$&$\lnot$&$($&Q &$\land$& $\lnot$&P&$)\}$&$\lor$&$\{$&R&$\lor$&$\lnot$&$\lnot$&Q&$\})$\\\hline
  T&F&F&F& &F&F&T&& F&F&F&T &&F &&F&F&F&T&F\\
\end{tabular}
\end{document}

truth table line

There are scripts which will produce the tables for you, but if you are learning logic, that's no good because you need to figure out the tables for yourself. If you are teaching logic, on the other hand, they can be very useful. They don't do the spacing adjustments I included here, either. At least, the one I used didn't.

share|improve this answer
    
Is there a program that has better logic support? – crocket Jan 10 at 4:29
    
@crocket Not that I know of. People either do something hackish with Word or something hackish with LaTeX. At least the latter looks better. But if you find something, do post an answer! – cfr Jan 10 at 4:31
2  
@crocket Are you aware of this site? – cfr Jan 10 at 4:32
    
No, I wasn't. I'll take a look. – crocket Jan 10 at 4:33
    
about automatic generation, does tex.stackexchange.com/a/162618/4686 help ? – jfbu Jan 11 at 8:56

You can type in the truth table with a rather natural syntax, provided some work behind the scenes is done.

\documentclass{article}
\usepackage{xparse,array}

\ExplSyntaxOn
\NewDocumentCommand{\truthtable}{mm}
 {
  \group_begin:
  \setlength{\arraycolsep}{0pt}
  \crocket_truth_make_preamble:n { #1 }
  \crocket_truth_make_body:n { #2 }
  \crocket_truth_make:
  \group_end:
 }

% variables
\tl_new:N \l__crocket_truth_preamble_tl
\tl_new:N \l__crocket_truth_first_tl
\tl_new:N \l__crocket_truth_body_tl
\seq_new:N \l__crocket_truth_rows_seq

% internal functions
\cs_new_protected:Nn \crocket_truth_make_preamble:n
 {
  \tl_clear:N \l__crocket_truth_preamble_tl
  \tl_clear:N \l__crocket_truth_first_tl
  % examine the first argument in order to build the table
  % preamble and the first row
  \tl_map_inline:nn { #1 }
   {
    \tl_if_in:VnTF \c_crocket_truth_delims_tl { ##1 }
     {% a delimiter is inserted in @{...} as a phantom (almost)
      % and not taken into account for the first row
      \tl_put_right:Nn \l__crocket_truth_preamble_tl { @{ \__crocket_truth_delim:n {##1} } }
     }
     {
      \tl_if_eq:nnTF { | } { ##1 }
       {% vertical bar is ignored in the rows, but it gives a | in
        % the table preamble (actually, quad | quad)
        \tl_put_right:Nn \l__crocket_truth_preamble_tl { @{\hspace{1em}}|@{\hspace{1em}} }
       }
       {% otherwise, c is added to the table preamble and
        % the item to the first row
        \tl_put_right:Nn \l__crocket_truth_preamble_tl { c }
        \tl_put_right:Nn \l__crocket_truth_first_tl { ##1 & }
       }
     }
   }
  % since rows will end with &, we add a dummy trailing column
  \tl_put_right:Nn \l__crocket_truth_preamble_tl { c } % empty last column
 }

\cs_new_protected:Nn \crocket_truth_make_body:n
 {% start building the table body
  \tl_clear:N \l__crocket_truth_body_tl
  % split the argument at \\
  \seq_set_split:Nnn \l__crocket_truth_rows_seq { \\ } { #1 }
  % and map it
  \seq_map_inline:Nn \l__crocket_truth_rows_seq
   {% each item is a row; map it to add &
    \tl_map_inline:nn { ##1 }
     {% but ignoring |
      \tl_if_eq:nnF { | } { ####1 }
       {
        \tl_put_right:Nn \l__crocket_truth_body_tl { \crocket_truth_tf:n { ####1 } & }
       }
     }
    % append the row terminator
    \tl_put_right:Nn \l__crocket_truth_body_tl { \\ }
   }
 }

\cs_new_protected:Nn \crocket_truth_make:
 {% make the table
  \exp_args:NnV \begin {array} \l__crocket_truth_preamble_tl
  % temporary define \__crocket_truth_delim:n to produce its argument
  \noalign { \cs_gset_eq:NN \__crocket_truth_delim:n \use:n }
  % output the first row
  \tl_use:N \l__crocket_truth_first_tl \\
  \hline
  % make \__crocket_truth_delim:n to deliver a phantom
  \noalign { \cs_gset_eq:NN \__crocket_truth_delim:n \hphantom }
  \tl_use:N \l__crocket_truth_body_tl
  \end{array}
 }

\cs_new_protected:Nn \crocket_truth_tf:n { \makebox[1.2em]{ $ \mathrm { #1 }$ } }

\tl_const:Nn \c_crocket_truth_delims_tl { ()[]\{\} }

\ExplSyntaxOff



\begin{document}

\[
\truthtable{
  P Q | (P \land Q)\lor P
}{
  T T | T T T T T\\
  T F | T F F T T
}
\]

\[
\truthtable{
  P Q R S | (\{ S \land \lnot ( Q \land \lnot P )\} \lor \{ R \lor \lnot \lnot Q \})
}{
  T F F F | F F T F F F T F F F F T F\\
  T F F F | F F T F F F T F F F F T F\\
  T F F F | F F T F F F T F F F F T F\\
  T F F F | F F T F F F T F F F F T F
}
\]
\end{document}

enter image description here

share|improve this answer
1  
Very nice: this should be a package. Though the P Q etc. shouldn't be in italics. At least, upright is the convention in logic text books. – cfr Jan 10 at 15:40
    
The document is nice, but the preamble causes vertigo. – crocket Jan 11 at 0:12
    
@crocket I'll add some explanations – egreg Jan 11 at 0:28

Another approach. You write the logic values immediately after the operator:

\def\use#1#2{\kern-1.2pt
  {\kern2pt#1\kern2pt\over{\ifx^#2^\else\rm\ifcase#2 F\or T\fi\fi}}\kern-1.2pt }
\def\n#1{\use #1{}}                   
\def\P#1{\use P#1}  \def\Q#1{\use Q#1} \def\R#1{\use R#1}  \def\S#1{\use S#1}
\def\formula{\use{}{}\vrule\use{}{}}
\def\And#1{\use \wedge#1} \def\Or#1{\use \vee#1}  \def\Not#1{\use\neg#1}

$$
\P1 \Q0 \R0 \S0
\formula
\n(\n\{ \S0 \And0 \Not1 \n(\Q0 \And0 \Not0 \P1 \n)\n\}
\Or0 
\n\{ \R0 \Or0 \Not0 \Not1 \Q0 \n\}\n)
$$

\bye
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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