0

I'm sorry for my English.

I just need to make a table with some long sentences. When I'm doing it I get the following result (see the picture), using the following code:

\begin{center}
\begin{tabular}{|c|c|c|c|c|}
    \hline
    Система & Кинетическая энергия $T$ & Потенциальная энергия $U$ & \multicolumn{2}{|c|}{Степени однородности} \\ \cline{4-5}
    & & & $h_T$ & $h_U$ \\ \hline
    Одномерный гармонический осциллятор & $\sim (\dot{x})^2$ & $\sim -x^2$ & 2 & 2 
\end{tabular}
\end{center}

The result How do I write the code to make the table fit right?

9
  • 2
    Please have also a look on minimal working example (MWE), for your next questions ;-)
    – user31729
    Commented Jul 31, 2015 at 22:32
  • @ChristianHupfer To be honest, I don't understand what you mean)
    – MGMKLML
    Commented Jul 31, 2015 at 22:39
  • Look at your example above: Nobody can compile it, because it's only a fragment.
    – user31729
    Commented Jul 31, 2015 at 22:41
  • @ChristianHupfer Sorry, what? I can compile without any mistakes what I wrote. The solution written by Bernard doesn't work bc of unknown reason. I've downloaded the required packages and just copied his code. By the way, the were an extra "}". I changed it but it still shows a mistake. I dunno the exact reason of that
    – MGMKLML
    Commented Jul 31, 2015 at 22:45
  • 3
    No, the fragment above, it's no document, only \begin{center}...\end{center} -- users have to build the \documentclass{...}...\begin{document}...\end{document} code around your fragment, this is extremely tedious. The harder this is, the fewer help you will get. And perhaps this is the reason why someone (no, not me!) downvoted your question.
    – user31729
    Commented Jul 31, 2015 at 22:47

3 Answers 3

5

You can use the makecell package: it allows for linebreaks in cells. Here is solution that uses tabularx to fit text width:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A ,T1]{fontenc}
\usepackage[russian]{babel}
\usepackage[showframe, nomarginpar]{geometry}
\usepackage{fourier, erewhon}
\usepackage{array, tabularx,multirow, makecell}
\setcellgapes{3pt}

\begin{document}

\begin{center}
\makegapedcells
\begin{tabularx}{\linewidth}{|c|c|c| >{\centering\arraybackslash}X | >{\centering\arraybackslash}X|}
\hline
& & & \multicolumn{2}{c|}{\makecell{Степени\\ однородности}} \\ \cline{4-5}
\multirowcell{-2}{Система} &\multirowcell{-3}{Кинетическая\\ энергия $T$} &\multirowcell{-3}{Потенциальная\\ энергия $U$} & $h_T$ & $h_U$ \\ \hline
\makecell{ Одномерный гармонический \\ осциллятор} & $\sim (\dot{x})^2$ & $\sim -x^2$ & 2 & 2 \\
\hline
\end{tabularx}
\end{center}

\end{document}

enter image description here

11
  • Thank you, but could you tell me what packages that you use are really necessary to build the code? Also, what is multirowcell and setcellgapes for?
    – MGMKLML
    Commented Jul 31, 2015 at 22:18
  • All, except geometry (I used it to show the frame), and fourier+erewhon, which are font packages that I loaded to have an easy access to cyrillic fonts.
    – Bernard
    Commented Jul 31, 2015 at 22:21
  • Okay. I will try the solution
    – MGMKLML
    Commented Jul 31, 2015 at 22:25
  • I've just slightly modified the code: there was a small error (\multicolumn{1}{|c|}{…} instead of \multicolumn{1}{c}{…}, resulting in a doublevertical rule.
    – Bernard
    Commented Jul 31, 2015 at 22:29
  • I get an error "Undefined control sequence" at the line "\end{tabularx}"
    – MGMKLML
    Commented Jul 31, 2015 at 22:34
3

Like this?

\documentclass{article}

\begin{document}

\begin{center}
\begin{tabular}{|p{1in}|p{1in}|p{1in}|p{1in}|c|}
        \hline
        Hello & a very long sentence $T$ & a very long sentence $U$ & \multicolumn{2}{|c|}{a very long sentence} \\ \cline{4-5}
        & & & $h_T$ & $h_U$ \\ \hline
        I'm afraid I don't speak Russian & $\sim (\dot{x})^2$ & $\sim -x^2$ & 2 & 2 
    \end{tabular}
\end{center} 


\end{document}
8
  • Yep, that works, but how to put the text in the center of the column?
    – MGMKLML
    Commented Jul 31, 2015 at 20:38
  • 1
    You could use *{4}{p{1in}|} to replicate the four first column descriptions more condensed.
    – user31729
    Commented Jul 31, 2015 at 20:49
  • @MGMKLML: You want to have centered text, but then wrapped if the text is too long for the cell width?
    – user31729
    Commented Jul 31, 2015 at 20:50
  • In case the O.P. wants to have centered, wrapped text: *{4}{C{1in}|} and \usepackage{array} with \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} (in the preamble of course)
    – user31729
    Commented Jul 31, 2015 at 20:52
  • @ChristianHupfer What is {4} for? Where and why should I use array usepackage?
    – MGMKLML
    Commented Jul 31, 2015 at 21:53
2

This is used explicitly from JPi's solution above, so I don't expect neither upvotes nor downvotes.

Regarding the vertical spacings/alignment, Bernards solution is better than mine of course.

\documentclass{article}

\usepackage{array}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{center}
\begin{tabular}{|*{4}{C{1in}|}c|}
        \hline
        Hello & a very long sentence $T$ & a very long sentence $U$ & \multicolumn{2}{c|}{a very long sentence} \\ \cline{4-5}
        & & & $h_T$ & $h_U$ \\ \hline
        I'm afraid I don't speak Russian & $\sim (\dot{x})^2$ & $\sim -x^2$ & 2 & 2 
    \end{tabular}
\end{center} 

\end{document}

enter image description here

3
  • For a head-scratching case of a downvote, see this answer. At least the downvoter chose not to be anonymous and actually gave a reason. Too bad the reason given for the downvote is invalid and is based on misreading the OP's main concern...
    – Mico
    Commented Sep 1, 2016 at 20:15
  • @Mico: We have some strange folks here and in my opinion it is getting stranger and more weird. (+1 there)
    – user31729
    Commented Sep 2, 2016 at 4:04
  • I fully agree! And many thanks for the upvote. :-)
    – Mico
    Commented Sep 2, 2016 at 5:03

You must log in to answer this question.

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