6

I can't find a way to vertically center a single line within a table cell.

Take a look at this screenshoot:

enter image description here

I'd like to vertically center the text within the bottom right cell and I just can't find a way to do it. I tried \usepackage{array} but I couldn't figure out how to apply the \centering\arraybackslash, which seems to align things vertically within cells, just to this single cell.

(I'm also not sure why the right border is missing for this cell.)

Here is the LaTeX code that generates this table:

\documentclass{article}

\usepackage[left=1cm,top=1cm,right=1cm,bottom=1cm,nohead,nofoot]{geometry}

\usepackage[pdftex]{hyperref}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{array}

\hypersetup{colorlinks}

\lstset{basicstyle=\ttfamily}

\pagestyle{empty}

% -----------------------------------------------------------------------

\begin{document}

\begin{tabular}{|p{5.5cm}|p{12.2cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\ 
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb|<< EOL| \\ 
\verb|foo| \\
\verb|bar| \\
\verb|baz| \\
\verb|EOL| & Redirect a bunch of lines to the stdin. \\
\hline
\end{tabular}

\end{document}
1
  • Please provide a complete code starting from \documentclass... .....\end{document}
    – user11232
    Commented Sep 9, 2012 at 23:11

2 Answers 2

6

You can use the m option provided by the array package to vertically centre the material in table columns. However, you have another problem. Using \\ without a preceding ampersand creates a row with only one column. This is why part of the right edge of the table is missing. Instead, you could use \par to insert a line break without moving to the next line of the table.

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|m{5.5cm}|m{12cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb+<< EOL+\par
\verb+foo+\par
\verb+bar+\par
\verb+baz+\par
\verb+EOL+
 & Redirect a bunch of lines to the stdin. \\
\hline
\end{tabular}
\end{document}

table

0
3

There are some mistakes. You are ending a tabularline with \. It should be \\. Also you are having two columns. Irrespective of the data, you have to use ampersands like (say)

\verb|foo|& \

This is the reason you are not getting right border.

To center the the particular cell you can use multirow. Put \usepackage{multirow} in the preamble and use

\verb+<< EOL+& \multirow{5}*{Redirect a bunch of lines to the stdin.} \\
    \verb+foo+&\\
    \verb+bar+&\\
    \verb+baz+&\\
    \verb+EOL+ &  \\
    \hline

Full code:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|p{5.5cm}|p{12cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb+<< EOL+& \multirow{5}*{Redirect a bunch of lines to the stdin.} \\
\verb+foo+&\\
\verb+bar+&\\
\verb+baz+&\\
\verb+EOL+ &  \\
\hline
\end{tabular}
\end{document}

enter image description here

0

You must log in to answer this question.