3

I want to create a table like this:

enter image description here

I used tablesgenerator.com to make it and got the following code:

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{cllll}
\hline
\multicolumn{2}{l}{\multirow{2}{*}{\textbf{\begin{tabular}[c]{@{}l@{}}list of targets -\textgreater\\ |list of sources\end{tabular}}}} & \multicolumn{3}{c}{\textbf{targets}}                                                                                                               \\ \cline{3-5} 
\multicolumn{2}{l}{}                                                                                                                   & \textbf{target1} & \textbf{target2 (additional info)} & \textbf{target3 (additional info)}                                                         \\ \cline{1-2}
\multirow{3}{*}{\textbf{sources}}            & \textbf{source1}                                                                        & description 10   & description 11                     & description 12                                                                             \\
                                             & \textbf{\begin{tabular}[c]{@{}l@{}}source2\\ (additional info)\end{tabular}}            & description 20   & description 21                     & \begin{tabular}[c]{@{}l@{}}description 22,\\ description 23,\\ description 24\end{tabular} \\
                                             & \textbf{source3}                                                                        & description 30   & description 31                     & \begin{tabular}[c]{@{}l@{}}description 32,\\ description 33,\\ description 34\end{tabular} \\ \cline{2-5} 
\end{tabular}
\end{table}

but the final result is like this:

enter image description here

It is stretched over the page borders and doesn't look anything like the source table.

Can anybody help me to make it look like in the first picture?

5

Some suggestions:

  • Don't use vertical lines in your tables (they're not needed! really!), and use fewer -- but well-spaced -- horizontal lines. Whitespace can be an excellent though unobtrusive (and therefore excellent) visual divider.

  • Don't overuse bold and colors as highlighting devices. ("When everything is highlighted, nothing is highlighted...") Do provide simple and appropriate structure to the header row(s). A well-designed table conveys information in both its header and its body. If you place too much emphasis on what's in the header, you run the risk of your readers not noticing what's in the body of the table.

  • Unless there's simply no other way to proceed, don't rotate text by 90 degrees. People universally dislike having to crane their necks.

  • Choose a descriptive caption, not "My caption". Can you come up with better, more descriptive terms than "Sources" and "Targets"? Brevity is good, but don't be too terse.

  • If a table with a clean, sober layout fails to convey the information you wish to get across, don't react by dressing up the table visually. Instead, ask yourself coolly what can be done to improve the content of the table. E.g., is the table too long, too cluttered, or rife with irrelevancies? If so, fix those problems immediately.

These ideas are implemented in the following code. It uses a tabularx environment, and all four columns are equally wide by design.

enter image description here

\documentclass{article}
\usepackage{booktabs,tabularx,ragged2e}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
\usepackage[skip=0.333\baselineskip]{caption}

\begin{document}
\begin{table}
\caption{Sources and targets}
\begin{tabularx}{\textwidth}{@{}*{4}{Y}@{}}
%% table header
\toprule
Sources & \multicolumn{3}{c@{}}{Targets} \\
\cmidrule(l){2-4}
        & Target 1 & Target 2 & Target 3 \\
\midrule
%% body of table
Source 1 & Description 10 
         & Description 11 
         & Description 12 \\
\addlinespace % <-- use whitespace as a simple yet very effective divider
Source 2 (additional info) 
         & Description 20 
         & Description 21
         & Description 22, Description 23, Description 24 \\
\addlinespace % ditto
Source 3 & Description 30 
         & Description 31
         & Description 32, Description 33, Description 34 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
  • Thank you Mico, this is almost it .. but the left part of the table (vertical text), and left upper corner (list of sources and list of targets with arrows) is also neccessary for me. This table that I have attached is only a dummy example, since the real data will be later populated and in that case these columns are important.. is it possible to have it like that ? .. all other suggestions are great.. you're completely right about vertical lines.. – marko3d Aug 13 '17 at 8:20
  • @marko3d - I based my answer on the information (and esp. the screenshot) you provided initially. In that screenshot, the text snippets with arrows and the rotated stuff didn't seem necessary (or even helpful -- remember that people detest having to crane their necks...). If that material is indeed necessary, it would be helpful if you posted a screenshot of a more elaborate table setup, to provide a better sense of where you need to go (table-wise, that is). – Mico Aug 13 '17 at 8:25
  • Because the data in the screenshot is only a placeholder for the data that will later be populated.Due to the nature of the data, I cannot publish it online.. :( however, if that is too complicated to make, I'll be satisfied with this also :) .. thank you – marko3d Aug 13 '17 at 8:30
1

If I have understood you correctly, this is what you were missing (I adapted the code from Mico)

enter image description here

\documentclass{article}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
\begin{table}
\caption{Sources and targets}
\begin{tabularx}{\textwidth}{p{1cm}@{}*{4}{Y}@{}}
%% table header
\toprule
\multicolumn{2}{r@{}}{List targets} $\rightarrow$ & \multicolumn{3}{c@{}}{Targets} \\
\cmidrule(l){3-5}
\multicolumn{2}{l@{}}{$\downarrow$ Sources}  & Target 1 & Target 2 & Target 3 \\
\midrule
%% body of table
\multirow{3}{2cm}{\rotatebox{90}{\parbox{\linewidth}{sources}}} &Source 1 & Description 10 
         & Description 11 
         & Description 12 \\
\addlinespace % <-- use whitespace as a simple yet very effective divider
&Source 2 (additional info) 
         & Description 20 
         & Description 21
         & Description 22, \par
           Description 23, \par
           Description 24 \\
\addlinespace % ditto
&Source 3 & Description 30 
         & Description 31
         & Description 32, \par 
           Description 33, \par 
           Description 34 \\    
\bottomrule
\end{tabularx}
\end{table}
\end{document}
  • thats exactly it.. and what about cell background color ? – marko3d Aug 13 '17 at 11:23
  • Add \usepackage[table]{xcolor}. To color a cell add '\cellcolor{lightgray}' to the specific cell before the entry. (Or '\rowcolor{lightgray}' to the beginning of the row) Yet the code given, espescially the command '\addlinespace' creates some problems with continuous cellcoloring – chris Aug 13 '17 at 11:55
  • Yes that white space I saw.. @Mico any chance of solving it? – marko3d Aug 13 '17 at 12:17

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.