0

I have a table of C++ operator precedence, and it's causing errors.

From what I can see, line 9 is a problem, but I cannot see why [] would not properly escape []

I'm sure there are plenty of errors, so any fixes would be greatly appreciated.

\input{preamble.tex}
\begin{document}
\mainmatter  
\begin{tabular}{l|l|l|l}
Precedence  & Operator & Description & Associativity            \\
1   &   ::  &   Scope resolution    &   Left-to-right       \\
2   &   ++   --                 \\
\(\) Function call                  \\
\[\]    Array subscripting              \\
.   Element selection by reference      \\
\-> Element selection through pointer   \\
    &   postincrement and decrement &   type\(\) type\{\}   Function-style type cast    \\
3   &   ++   --         \\
+   -   Unary plus and minus        \\
!   ~   Logical NOT and bitwise NOT \\
(type)  C-style type cast           \\
*   Indirection (dereference)       \\
\&  Address-of              \\
sizeof                      \\
new                         \\
delete  
&   Preincrement and decrement  &   Right-to-left           \\
4   &   .*   ->*    &   Pointer to member   &   Left-to-right   \\
5   &   *   /   \%  &   Multiplication, division, and remainder \\
6   &   +   -       &   Addition and subtraction            \\
7   &   <<   >> &   Bitwise left shift and right shift      \\
8   &   <   <=  
        >   >=
&   Relational operators            \\
    Relational operators >  ≥ respectively  \\
9   &   ==   != &   For relational = and ≠ respectively \\
10  &   \&      &   Bitwise AND \\
11  &   ^       &   Bitwise XOR     \\
12  &   |       &   Bitwise OR  \\
13  &   \&\&        &   Logical AND \\
14  &   ||      &   Logical OR  \\
15  &   ?:
        throw
        =   Direct assignment (provided by default for C++ classes)
        +=   -= Assignment by sum and difference
        *=   /=   \%=   Assignment by product, quotient, and remainder
        <<=   >>=   Assignment by bitwise left shift and right shift
        \&=   ^=   |=   Assignment by bitwise AND, XOR, and OR
        &   Ternary &   Right-to-left   \\
16  &   ,   &   Comma   &   Left-to-right
\end{tabular}
\end{document}                          

When I get rid of the backslashes on the parentheses, there is still an error on line 9:

! Missing number, treated as zero. } l.9 [] Array subscripting \ ? ! Illegal unit of measure (pt inserted).

The new (stripped) code is:

\input{preamble.tex}
\begin{document}
\mainmatter  
\begin{tabular}{l|l|l|l}
Precedence  &    Operator &  Description    &   Associativity           \\
1   &   ::  &   Scope resolution    &   Left-to-right       \\
2   &   ++   --                 \\
() Function call                    \\
[] Array subscripting               \\
. Element selection by reference        \\
-> Element selection through pointer    \\
    &   postincrement and decrement &   Function-style type cast    \\

OK, given the answer I cleaned up the table a lot, including making it standalone so anyone can copy it to learn from.

\documentclass{standalone}
\begin{document}
%\tiny
%\begin{tabular}{l|l|l|l}
\begin{tabular}{llll}
{\huge Precedence}  &    {\huge Operator}   &   {\huge Description} &   {\huge Associativity}           \\ \hline
1   &   \verb$::$   &    Scope resolution    &   Left-to-right              \\
2   &   \verb$++$   \verb$--$   &   pre-increment and decrement &       \\ \hline
    &   \verb$()$   &   Function call       &                       \\
    &   \verb$[]$   &   Array subscripting  &                       \\
    &   \verb$.$    &   Object member   &                       \\
    &   \verb$->$   &   Element selection through pointer               \\
    &   \verb$()$   &   Function-style type cast                        \\ \hline
3   &   \verb$++$   \verb$--$   &   post-increment and decrement & Right-to-left    \\
    &   \verb$+$   \verb$-$ &   Unary plus and minus                \\
    &   \verb$!$   \verb$~$ &   Logical NOT and bitwise NOT         \\
    &   (type)          &   C-style type cast                           \\
    &   \verb$*$    &   Pointer dereference                     \\
    &   \verb$&$    &   Address-of                                          \\
    &   sizeof      &   The number of atomic units of memory used           \\
    &   new         &   dynamic memory allocation                   \\
    &   delete      &   dynamic memory deallocation                 \\
4   &   \verb$.*$   \verb$->*$  &   Pointer to member   &   Left-to-right       \\ \hline
5   &   \verb$*$   \verb$/$   \verb+\%+  &  Multiplication, division, and remainder \\ \hline
6   &   \verb$+$   \verb$-$       & Addition and subtraction                            \\ \hline
7   &   \verb$<<$   \verb$>>$ &   Bitwise left shift and right shift                \\ \hline
8   &   \verb$<$   \verb$<=$    &   Relational operators \verb$>$  \verb$≥$ \\
    &   \verb$>$  \verb$>=$ &   Relational operators \verb$\greaterequal$  \verb$≥$ \\ \hline
9   &   \verb$==$   \verb$!=$ &   Relational equality and inequality \verb$≠$       \\ \hline
10  &   \verb$&$        & Bitwise AND                           \\ \hline
11  &   \verb$^$        &   Bitwise XOR                         \\ \hline
12  &   \verb$|$        &   Bitwise OR                          \\ \hline
13  &   \verb$&&$       &   Logical AND                         \\ \hline
14  &   \verb$||$       &   Logical OR                              \\ \hline
15  &   \verb$?:$       & ternary        &   Right-to-left              \\
    &   throw           & throw an exception                        \\
    &   \verb$=$        & assignment                            \\
    &   \verb$+=$   \verb$-=$   &   additive assignment                 \\
    &   \verb$*=$   \verb$/=$   \verb$\%=$  & Multiplicative assignment         \\
    &   \verb$<<=$   \verb$>>=$   &  Bitwise left shift and right shift assignment  \\
    &   \verb$\&=$   \verb$^=$   \verb$|=$  & Bitwise AND, XOR, and OR assignment   \\ \hline
16  &   \verb$,$         &   Comma   &   Left-to-right                  \\
\end{tabular}
\end{document} 
  • \(\) and \[\] are math delimiters. Do you want to show them as such? – user11232 May 15 '15 at 1:55
  • Simply use () and [] (No need to escape them) – Aditya May 15 '15 at 2:07
  • Now it says it expects a number? (see edited text) – Dov May 15 '15 at 2:21
  • Not strictly relevant and others may feel differently, but it's a little unhelpful if you don't include your preamble in full in your MWE. This is partly because your problem could be there (I doubt it's there here, but you might have a package clash or something), and partly because providing a full, proper MWE allows others to use your code and play with it and then they can reproduce what you're seeing and experiment with solutions without having to guess what you have, or rewrite your .tex file from clues in the question. Anyway, not such a big deal in this case, but maybe next time? – Au101 May 15 '15 at 2:38
  • @au101 I will edit the code and put it back in. – Dov May 16 '15 at 17:26
4

I would suggest you use the inline verbatim command to display all the special operators. The code is below and when you compile it you will discover it is very ugly. And you have many content errors to fix but this is not a C++ programming forum.

\documentclass{standalone}
\begin{document}
\tiny
\begin{tabular}{l|l|l|l}
Precedence  & Operator & Description & Associativity            \\
1   &   \verb+::+  &   Scope resolution    &   Left-to-right       \\
2   &   \verb-++-   \verb+--+ &&                \\
\verb+()+ Function call  &&&                \\
\verb+[]+    Array subscripting  &&&            \\
\verb+.+   Element selection by reference &&&     \\
\verb+->+ Element selection through pointer &&&  \\
    &   postincrement and decrement &   type\(\) type\{\}   Function-style type cast    \\
3   &   \verb-++-   \verb+--+         \\
\verb-+-   \verb+-+   Unary plus and minus        \\
\verb+!+   \verb+~+   Logical NOT and bitwise NOT \\
(type)  C-style type cast           \\
\verb+*+   Indirection (dereference)       \\
\verb+&+  Address-of              \\
sizeof                      \\
new                         \\
delete  
&   Preincrement and decrement  &   Right-to-left           \\
4   &   \verb+.*   \verb+->*    &   Pointer to member   &   Left-to-right   \\
5   &   \verb+*+   \verb+/+   \verb+\%+  &   Multiplication, division, and remainder \\
6   &   \verb-+-   \verb+-+       &   Addition and subtraction            \\
7   &   \verb+<<+   \verb+>>+ &   Bitwise left shift and right shift      \\
8   &   \verb+<+   \verb+<=+  
        \verb+>+   \verb+>=+
&   Relational operators            \\
    Relational operators \verb+>  \verb+≥+ respectively  \\
9   &   \verb+==+   \verb+!=+ &   For relational \verb+=+ and \verb+≠+ respectively \\
10  &   \verb+\+ &      &   Bitwise AND \\
11  &   \verb+^+       &   Bitwise XOR     \\
12  &   \verb+|+       &   Bitwise OR  \\
13  &   \verb+&&+        &   Logical AND \\
14  &   \verb+||+      &   Logical OR  \\
15  &   \verb+?:+
        throw
        \verb+=+   &Direct assignment (provided by default for C++ classes)\\
        \verb-+=-   \verb+-=+& Assignment by sum and difference\\
        \verb+*=+   \verb+/=+   \verb+\%=+  & Assignment by product, quotient, and remainder\\
        \verb+<<=+   \verb+>>=+   &Assignment by bitwise left shift and right shift\\
        \verb+\&=+   \verb+^=+   \verb+|=+  & Assignment by bitwise AND, XOR, and OR\\
        &   Ternary &   Right-to-left   \\
16  &   \verb+,+   &   Comma   &   Left-to-right\\
\end{tabular}
\end{document} 
  • If you see content errors in the operators I would be curious to know, I think the information is pretty correct even if the latex is bad! – Dov May 16 '15 at 11:31
  • @Dov Just a couple of content issues to get you started on very close editing. Trust me the compiler is very picky and unforgiving. ... ++ and -- are not the only precedence 2 but also () function call to type() case and (omitted) type{} ... Also pay very careful attention to where you have placed the horizontal lines. – R. Schumacher May 16 '15 at 18:08
3

You've got a number of errors in your code.

To begin with, certain characters have special meaning. Of those that you've used, they would be ^, &, ~, and \. For these you need to use special control characters and macros.

\^{}       for `^`
\&         for `&`
\backslash for `\`
$\sim$     for `~`

The character ^ is used in math mode to create a superscript.

The character & has various uses. Here in the table is marks the border between columns.

The character \ followed by white space creates white space.

The character ~ is similar to \, but it creates whitespace that will not be broken across lines or page boundaries.

If you're going to be typesetting computer languages you might want to look at the listings package.

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.