0

I want to get a table that looks like

enter image description here

However, when using

\begin{tabular}{|p{2cm}|p{3cm}|p{3cm}|p{3cm}|}
\hline Time-Scale & Mechanical Machine State Variables & Electrical Machine  State Variables  & Power Electronics  State Variables  \\ 
\hline Mechanical Machine (Slowest)
& Dynamic & Instantaneous & Instantaneous \\ 
\hline Electrical Machine (Middle)
& Frozen & Dynamic & Instantaneous \\ 
\hline Power Electronics (Fastest)
& Frozen & Frozen & Dynamic \\ 
\hline 
\end{tabular}

I get a bad looking table:

enter image description here

How can I make the text in each box centered and how can I divide the text in one box into multiple lines? (When I tried using \ between words, it was treating that as a new table entry.)

From a previous post, I see that using tabularx instead of tabular is one possible solution. Is that the recommend solution?

Instead of manually entering new lines, is there a way to fix the column width and prevent words from appearing partly on the end of one line and partly on the beginning of the next?

Thank you,

Kevin

1
  • The p{} option is equivalent to putting \parbox[t]{} into some other field. However, you probably want \parbox[c]{} or \parbox[b]{}. Aug 7, 2015 at 13:39

1 Answer 1

2
% arara: pdflatex

\documentclass{article}
\usepackage{array}
\newcommand{\specialcell}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline 
        \bfseries Time-Scale & \bfseries\specialcell{Mechanical\\Machine State\\Variables} & \bfseries\specialcell{Electrical\\Machine  State\\Variables}  & \bfseries\specialcell{Power\\Electronics  State\\Variables}  \\ 
        \hline 
        \specialcell{Mechanical\\Machine\\(Slowest)} & Dynamic & Instantaneous & Instantaneous \\ 
        \hline 
        \specialcell{Electrical\\Machine\\(Middle)} & Frozen & Dynamic & Instantaneous \\ 
        \hline 
        \specialcell{Power\\Electronics\\(Fastest)} & Frozen & Frozen & Dynamic \\ 
        \hline 
    \end{tabular}
\end{table}
\end{document}

enter image description here


Nicer to read would be:

\documentclass{article}
\newcommand{\specialcell}[1]{\begin{tabular}{@{}l@{}}#1\end{tabular}}
\usepackage{booktabs}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{*{4}{l}}
        \toprule 
        Time-Scale & \specialcell{Mechanical\\Machine State\\Variables} & \specialcell{Electrical\\Machine  State\\Variables}  & \specialcell{Power\\Electronics  State\\Variables}  \\ 
        \midrule 
        \specialcell{Mechanical\\Machine\\(Slowest)} & Dynamic & Instantaneous & Instantaneous \\\addlinespace 
        \specialcell{Electrical\\Machine\\(Middle)} & Frozen & Dynamic & Instantaneous \\\addlinespace
        \specialcell{Power\\Electronics\\(Fastest)} & Frozen & Frozen & Dynamic \\ 
        \bottomrule 
    \end{tabular}
\end{table}
\end{document}

enter image description here

4
  • Thanks, working perfectly. As a minor question, how I center the table title? I added \caption {Table Title} right after \centering, but the Table Title appears flush to the left rather than centered.
    – KevinB
    Aug 7, 2015 at 14:05
  • We will need to know your \documentclass for this. It seems as this is the desired look by your template so maybe you should not change that. The topic on how to manipulate the caption has been discussed many times on this site here. Please do a query.
    – LaRiFaRi
    Aug 7, 2015 at 14:09
  • @LaRiFaRi: Looks like you reinvented \makecell?
    – user31729
    Aug 7, 2015 at 21:15
  • @ChristianHupfer Really? Don't know that. Hehe, I will have to search for that next week. I am always using that 'special cell' which I stole from some code by egreg, I believe. Not sure...
    – LaRiFaRi
    Aug 7, 2015 at 22:33

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.