1

I'm not sure, but the third column of a table I'm trying to create is too wide... I've tried using the 'p' option, or even footnotesize, but the column is still too big to show in beamer slide, and there's also too much useless space in it.

Here's my MWE:

\documentclass{beamer}
\mode<presentation> {
\usetheme{AnnArbor}
\usecolortheme{beaver}
}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{array}

\begin{document}
\begin{table}
        \footnotesize
        \begin{tabular}{| c | c | c p{5cm} |}
                        \hline
                        \textbf{Operator} & \textbf{Description} & \textbf{Example} \\
                        \hline
                        \texttt{del dict[key]} & Delete the value associated with \texttt{key}. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                del my_dict['a']  # my_dict is now {'b': 2}
                        \end{lstlisting} \\
                        \hline
                        \texttt{dict.keys()} & Return a view object of all keys. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                keys = my_dict.keys()  # dict_keys(['a', 'b'])
                        \end{lstlisting} \\
                        \hline
                        \texttt{dict.values()} & Return a view object of all values. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                values = my_dict.values()  # dict_values([1, 2])
                        \end{lstlisting} \\
                        \hline
                        \texttt{dict.items()} & Return a view object of all key-value pairs. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                items = my_dict.items()  # dict_items([('a', 1), ('b', 2)])
                        \end{lstlisting} \\
                        \hline
                        \texttt{key in dict} & Check if \texttt{key} exists in the dictionary. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                exists = 'a' in my_dict  # True
                        \end{lstlisting} \\
                        \hline
                        \texttt{dict.clear()} & Remove all items from the dictionary. & \begin{lstlisting}[language=Python]
                my_dict = {'a': 1, 'b': 2}
                my_dict.clear()  # my_dict is now {}
                        \end{lstlisting} \\
                        \hline
                \end{tabular}
        % }
        \caption{Python Dictionary Operations}
        \label{tab:python_dict_operations}
\end{table}
    
\end{document}

Here's a screenshot of what I see: enter image description here

How can I solve this?

8
  • 2
    you have made the 4th column a p but the third column is still c so single line as wide as the longest line Commented Jul 11 at 10:33
  • 2
    you can not have display environments like listing in a c column Commented Jul 11 at 10:38
  • 1
    And what also adds to spoiling the fun is that the blanks in front of the Python statements are considered part of the source code (which isn't unreasonable if you're programming Python)
    – Ronald
    Commented Jul 11 at 10:40
  • You're missing a frame environment (use it with the fragile option to allow source code). Commented Jul 11 at 10:41
  • 1
    @Anoldmaninthesea. Maybe have a look at texdoc.org/serve/booktabs.pdf/0 for some information how to design professional looking tables Commented Jul 11 at 12:16

1 Answer 1

1

To summarise the various comments in a community-wiki answer:

  • you shouldn't typeset anything in beamer without a frame environment (notice the incorrect vertical alignment and broken numbering). Use the fragile frame option to allow source code.
  • use a fixed-width column, e.g. p{...}, for the one which contains the listings
  • you will also save some space if you use a fixed-width column for the second column
  • don't indent code inside of lstlisting unless the indentation is part of the code (or use the gobble=... option to remove as many characters as needed)
  • allow line breaks in listsing: \lstset{breaklines}
  • adjust the above-/belowskip of listings to save some space
  • use a smaller font size or change the aspect ratio of your presentation if you need to squeeze so much information on a single slide. Alternatively, consider splitting it into two slides.

\documentclass{beamer}
\mode<presentation> {
\usetheme{AnnArbor}
\usecolortheme{beaver}
}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{array}


\lstset{
  basicstyle=\ttfamily\scriptsize,
  breaklines
}

\begin{document}
\begin{frame}[fragile]
\begin{table}
\scriptsize
\lstset{
  aboveskip=-.7\baselineskip,
  belowskip=0pt
  }
\begin{tabular}{| l | >{\raggedright}p{2cm} | p{6.76cm} |}
\hline
\textbf{Operator} & \textbf{Description} & \textbf{Example} \\
\hline
\texttt{del dict[key]} & Delete the value associated with \texttt{key}. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
del my_dict['a']  # my_dict is now {'b': 2}
\end{lstlisting} \\
\hline
\texttt{dict.keys()} & Return a view object of all keys. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
keys = my_dict.keys()  # dict_keys(['a', 'b'])
\end{lstlisting} \\
\hline
\texttt{dict.values()} & Return a view object of all values. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
values = my_dict.values()  # dict_values([1, 2])
\end{lstlisting} \\
\hline
\texttt{dict.items()} & Return a view object of all key-value pairs. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
items = my_dict.items()  # dict_items([('a', 1), ('b', 2)])
\end{lstlisting} \\
\hline
\texttt{key in dict} & Check if \texttt{key} exists in the dictionary. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
exists = 'a' in my_dict  # True
\end{lstlisting} \\
\hline
\texttt{dict.clear()} & Remove all items from the dictionary. & \begin{lstlisting}[language=Python]
my_dict = {'a': 1, 'b': 2}
my_dict.clear()  # my_dict is now {}
\end{lstlisting} \\
\hline
\end{tabular}
% }
\caption{Python Dictionary Operations}
\label{tab:python_dict_operations}
\end{table}
\end{frame}

\end{document}

enter image description here

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .