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?

  • 2
    Please have also a look on minimal working example (MWE), for your next questions ;-) – user31729 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. – user31729 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. – user31729 Jul 31 '15 at 22:47
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

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

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.