2

I use tabularx to break lines automatically in tabular. Here is my code.

\begin{table}[h]
\caption{A table}
\begin{tabularx}{\textwidth}{|l|X|}
\hline
\multicolumn{1}{|c|}{\textbf{Term}} & \multicolumn{1}{c|}{\textbf{Description}} \\
\hline
key1 & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
\hline
\end{tabularx}
\end{table}

It works in \documentclass[a4paper]{article} but fails in \documentclass[conference]{IEEEtran} (two columns). Here is the result.

enter image description here

  • your very long text is not realy long – touhami Jul 8 '15 at 17:05
  • @touhami there are 8 copies of A very long text. but it only shows 5 copies. – SparkAndShine Jul 8 '15 at 17:07
  • Ok sorry but for me it works well with10 – touhami Jul 8 '15 at 17:08
  • 1
    use \linewidth rather than \textwidth – touhami Jul 8 '15 at 17:16
  • 1
    @SparkandShine, you select textwidth' for width of table. In two colummarticle (anyone) this is width of two column. Use \begin{tabularx}{\hsize}{|l|X|}` instead \begin{tabularx}{\textwidth}{|l|X|}` should sove your problem (if I correctly understand them). – Zarko Jul 8 '15 at 17:16
4

Very simple: replace \textwidth with \linewidth. I took the opportunity to improve the vertical spacing of the rows of the table, and to propose a solution without vertical rules, that looks better, in the opinion of many:

\documentclass{ieeetran}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{booktabs, caption, tabularx, makecell}
\renewcommand\theadfont{\bfseries}
\setcellgapes{4pt}
\begin{document}
\lipsum[1]
\begin{table}[h]
  \caption{A table}
  \makegapedcells
  \begin{tabularx}{\linewidth}{|l|X|}
    \hline
    \multicolumn{1}{|c|}{\textbf{Term}} & \multicolumn{1}{c|}{\textbf{Description}} \\
    \hline
    key1 & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
    \hline
  \end{tabularx}
\end{table}
\lipsum[2-5]
\begin{table}[h]
  \caption{A table}
  \begin{tabularx}{\linewidth}{lX}
    \toprule
    \thead{Term} & \thead{Description} \\
    \midrule
    key1 & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
    \bottomrule
  \end{tabularx}
\end{table}
\lipsum[6]
\end{document} 

enter image description here

  • 1
    From a coding point of view, it might be even clearer to use \columnwidth, rather than \linewidth. – Mico Jul 8 '15 at 23:35
  • 1
    @Mico: Surely, but coding \inewidth works in all circumstances (poly and monostyles), όχι; – Bernard Jul 8 '15 at 23:43
2

You can get this:

enter image description here

\documentclass[conference]{IEEEtran}
\usepackage{tabularx,lipsum}
\begin{document}

\lipsum[1-2]
\begin{table}[h]
\renewcommand{\arraystretch}{1.2}
\caption{A table}
\begin{tabularx}{\linewidth}{|l|X|}
\hline
\multicolumn{1}{|c|}{\textbf{Term}} & \multicolumn{1}{c|}{\textbf{Description}} \\
\hline \hline
key1 & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
\hline
\end{tabularx}
\end{table}
\lipsum[3-8]
\end{document}

Use tabularx to be able to utilize all of the available width. First column is set to l since it is short, the other is set using X to automatically break within the remaining available width. \linewidth is not equal to \textwidth use the first in two-column documents.

NOTE: You could've used booktabs since it is more beautiful, but, the style I used is most common in IEEE journals and conferences.

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.