Centering Text Horizontally and Vertically in LaTeX

67,669

Solution 1

You can make a new column type, or simply add >{\centering\arraybackslash} before m{6cm} for the two image columns.

For example:

\newcolumntype{C}{>{\centering\arraybackslash} m{6cm} }  %# New column type
\begin{tabular}{m{1cm}CC}                                %# Table with two of them
...

The > directive lets you basically inject the contained code before each entry in that column. We need the \arraybackslash to deal with incompatibility between the centering environment and the tabular environment. [More info can be found here.] 1

Solution 2

I use \dummyimage, because I have no im.png. Replace it with \includegraphics{im.png}.

\font\dummyfont = cmr10 at 100pt
\def\dummyimage{{\vbox to 100pt{\vfil\hbox to 100pt{\hfil\dummyfont A\hfil}\vfil}}}

\hfil\vbox{
\halign{&\hfil\ $\vcenter{\hbox{#}}$\strut \ \hfil\cr
&a&b\cr 
1&\dummyimage&\dummyimage\cr
2&\dummyimage&\dummyimage\cr
}}
Share:
67,669
Momo
Author by

Momo

I work as a software development manager, working mostly with C#, Azure and SQL Server.

Updated on July 06, 2020

Comments

  • Momo
    Momo almost 4 years

    I would like to produce the following:

          a       b
        xxxxx   xxxxx
     1  xxxxx   xxxxx
        xxxxx   xxxxx
    
        xxxxx   xxxxx
     2  xxxxx   xxxxx
        xxxxx   xxxxx
    

    Where the blocks of 'x's are images, and 'a', 'b', '1' and '2' are text.

    Here are my two attempts so far:

    \begin{figure}
    \begin{center}
    \begin{tabular}{ccc}
     & a & b \\
    1 & \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} &
        \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} \\
    2 & \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} &
        \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} \\
    \end{tabular}
    \end{center}
    \end{figure}
    

    Which produces:

          a       b
        xxxxx   xxxxx
        xxxxx   xxxxx
     1  xxxxx   xxxxx
    
        xxxxx   xxxxx
        xxxxx   xxxxx
     2  xxxxx   xxxxx
    

    And

    \begin{figure}
    \begin{center}
    \begin{tabular}{m{1cm}m{6cm}m{6cm}}
     & a & b \\
    1 & \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} &
        \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} \\
    2 & \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} &
        \subfloat[]{\includegraphics[width=0.47\textwidth]{im.png}} \\
    \end{tabular}
    \end{center}
    \end{figure}
    

    Which produces:

        a       b
        xxxxx   xxxxx
     1  xxxxx   xxxxx
        xxxxx   xxxxx
    
        xxxxx   xxxxx
     2  xxxxx   xxxxx
        xxxxx   xxxxx
    
  • Charles Stewart
    Charles Stewart about 14 years
    @Svante: Yes. I don't see anything in Alexey's answer that would cause problems in Latex.