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?

lockstep
  • 250,273
MGMKLML
  • 185
  • 2
    Please have also a look on minimal working example (MWE), for your next questions ;-) –  Jul 31 '15 at 22:32
  • @ChristianHupfer To be honest, I don't understand what you mean) – MGMKLML Jul 31 '15 at 22:39
  • Look at your example above: Nobody can compile it, because it's only a fragment. –  Jul 31 '15 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 Jul 31 '15 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. –  Jul 31 '15 at 22:47
  • @ChristianHupfer I know I seem to be very stupid but not so stupid to try to compile the document withouth the things you pointed out. – MGMKLML Jul 31 '15 at 22:54
  • I get a little bit angry, actually: I am not stupid too to try to compile a fragment: You leave the efforts to make working document to other users -- that's your task actually, not for users willing to help you ;-) –  Jul 31 '15 at 22:57
  • @ChristianHupfer I get you. Sorry, I will definitely behave myself the right way the next time and rectify this misunderstanding. – MGMKLML Jul 31 '15 at 23:02
  • Alright then! It's much easier for all us –  Jul 31 '15 at 23:03

3 Answers3

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

Bernard
  • 271,350
  • 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 Jul 31 '15 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 Jul 31 '15 at 22:21
  • Okay. I will try the solution – MGMKLML Jul 31 '15 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 Jul 31 '15 at 22:29
  • I get an error "Undefined control sequence" at the line "\end{tabularx}" – MGMKLML Jul 31 '15 at 22:34
  • Sorry, I've found out what it is, it was my mistake, I forgot to add

    \setcellgapes{3pt}

    – MGMKLML Jul 31 '15 at 22:48
  • This is how it looks now and it looks strange: the result. – MGMKLML Jul 31 '15 at 22:50
  • @MGMKLML: I've just checked the code: there was a spurious } (a problem with my editor). Would you please test the corrected code? – Bernard Jul 31 '15 at 23:39
  • @Bernard, its look better if you use \multicolumn{2}{c|}{Степени\\ однородности} (with only right|). Otherwise, nice solution. – Zarko Jul 31 '15 at 23:53
  • @Zarko: I changed {|c|} to {c} a moment ago, but was mistaken by the showframe option of geometry. Thanks for pointing it. – Bernard Aug 01 '15 at 00:09
  • @MGMKML: I see I forgot to answer some of your question: the makecell package has a number of commands that allow for linebreaks inside cells, and a common formatting: \thead, \makecell,\multirowtheadand\multirowcell. Some vertical padding (fixed with\setcellgapes) around rows can be obtained with the\setcellgapesswitch. You also can have variable-width lines with the\Xhlineand\Xcline` commands. – Bernard Aug 01 '15 at 17:52
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}
JPi
  • 13,595
  • Yep, that works, but how to put the text in the center of the column? – MGMKLML Jul 31 '15 at 20:38
  • 1
    You could use *{4}{p{1in}|} to replicate the four first column descriptions more condensed. –  Jul 31 '15 at 20:49
  • @MGMKLML: You want to have centered text, but then wrapped if the text is too long for the cell width? –  Jul 31 '15 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) –  Jul 31 '15 at 20:52
  • @ChristianHupfer What is {4} for? Where and why should I use array usepackage? – MGMKLML Jul 31 '15 at 21:53
  • @MGMKLML: {4} --> 4 columns with the same column type. (since 4 four columns have text that should be wrapped. And you need array package for the new column type C (Center and wrap) -- it's only a suggestion, I won't edit JPi's answer of course –  Jul 31 '15 at 21:55
  • @ChristianHupfer So I should use array instead of tabular? Sounds hard – MGMKLML Jul 31 '15 at 21:58
  • @MGMKLML: No, no, array is the package name, not the environment name. It's just useful for having new column types! –  Jul 31 '15 at 22:00
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

  • 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 Sep 01 '16 at 20:15
  • @Mico: We have some strange folks here and in my opinion it is getting stranger and more weird. (+1 there) –  Sep 02 '16 at 04:04
  • I fully agree! And many thanks for the upvote. :-) – Mico Sep 02 '16 at 05:03