TransWikia.com

Problem with vertical alignment in a table for a header

TeX - LaTeX Asked by P.M. on September 24, 2020

I’m really stuck and I would appreciate any advice. I need to design a header for a technical report that looks like this:

header

The first column is 2.5 cm in width, the second column has 11cm, the third 1cm and the fourth 1cm. The height of the first row is 1.3 cm and the second row has 1 cm.
I need the header to have this measures as we have some people using open office and some people using LaTex.

My MWE so far, looks like this:

documentclass[a4paper,10pt]{article}
usepackage{graphicx}
usepackage{hyperref}
usepackage{array}
usepackage{makecell}
usepackage{multirow}
usepackage{multicol}
usepackage{fancyhdr}
usepackage[hmargin=2cm,top=4cm,headheight=6cm,footskip=65pt]{geometry}
usepackage[version=4]{mhchem} % loads some package that allows to use "arrayrulewidth"
usepackage{lipsum}
    
% new columns definition
newcolumntype{M}[1]{@{}>{centeringarraybackslash}m{#1}@{}}
newcolumntype{N}{@{}m{0pt}@{}}
                
pagestyle{fancy}
renewcommandheadrulewidth{0pt} % no line between document and header
fancyhf{} % clear header and footer
fancyhead[C]{%
    begin{tabular}{|M{2.5cm-1.25arrayrulewidth}|M{11cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|N}
        hline
        multirow{2}{*}{includegraphics[height=2.2cm,width=2.2cm]{example-image}} & makecell{Report  footnotesize{from a research center}} & multicolumn{2}{M{2cm-1.5arrayrulewidth}|}{makecell{Deliverable 2.0}} & rule{0pt}{1.3cm}
        cline{2-4} 
        & multicolumn{2}{M{12cm-1.5arrayrulewidth}|}{makecell{Report title  and number}}     & makecell{Page  thepage} & rule{0pt}{1cm}
        hline
    end{tabular}
}

begin{document}

section{Section title}
%% add this if you want the fancy style also on the first page of a chapter:
thispagestyle{fancy}
subsection{Subsection title}
lipsum[1-10]

end{document}

enter image description here

The problem is that the image in the left cell is squared and I don’t know how to place it in the middle (vertical alignment).
I would appreciate any input that you can provide.

There are vertical alignment issues in the other cells, as they appear to be below the center of the cell. But it is not as troublesome as the image in the left cell.

3 Answers

One workaround for your main issue is using minipage for placing the image inside the table cell with centering and the full width of the cell. You can then use vspace to adjust its vertical alignment. You can change the number of -0.8 at vspace{-0.8baselineskip} for shifting the image up or down in the cell.

You then obtain something like this:

enter image description here

Full code

documentclass[a4paper,10pt]{article}
usepackage{graphicx}
usepackage{hyperref}
usepackage{array}
usepackage{makecell}
usepackage{multirow}
usepackage{multicol}
usepackage{fancyhdr}
usepackage[hmargin=2cm,top=4cm,headheight=6cm,footskip=65pt]{geometry}
usepackage[version=4]{mhchem} % loads some package that allows to use "arrayrulewidth"
usepackage{lipsum}

% new columns definition
newcolumntype{M}[1]{@{}>{centeringarraybackslash}m{#1}@{}}
newcolumntype{N}{@{}m{0pt}@{}}

pagestyle{fancy}
renewcommandheadrulewidth{0pt} % no line between document and header
fancyhf{} % clear header and footer
fancyhead[C]{%
    begin{tabular}{|M{2.5cm-1.25arrayrulewidth}|M{11cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|N}
        hline
        multirow{2}{*}{begin{minipage}{1linewidth}
                centering
                vspace{-0.8baselineskip}
                includegraphics[height=2.2cm,width=2.2cm]{example-image}
                end{minipage}}
                & makecell{Report  footnotesize{from a research center}} & multicolumn{2}{M{2cm-1.5arrayrulewidth}|}{makecell{Deliverable 2.0}} & rule{0pt}{1.3cm}
        cline{2-4} 
        & multicolumn{2}{M{12cm-1.5arrayrulewidth}|}{makecell{Report title  and number}}     & makecell{Page  thepage} & rule{0pt}{1cm}
        hline
    end{tabular}
}

begin{document}
    
    section{Section title}
    %% add this if you want the fancy style also on the first page of a chapter:
    thispagestyle{fancy}
    subsection{Subsection title}
    lipsum[1-10]
    
end{document}

Correct answer by hesham on September 24, 2020

Like this?

enter image description here

documentclass[a4paper,10pt]{article}
usepackage[hmargin=2cm,top=4cm,headheight=6cm,footskip=65pt]{geometry}
usepackage{graphicx}
usepackage{array, makecell, multirow}
newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
usepackage{fancyhdr}
pagestyle{fancy}
fancyhf{} % clear header and footer
renewcommandheadrulewidth{0pt} % no line between document and header
fancyhead[C]{centering%
    begin{tabular}{|P{25mm}|c |P{1cm}|P{1cm}|@{}c@{}}
        hline
multirow{5}{=}[4.4mm]{centeringincludegraphics[height=22mm, width=22mm]{example-image}}
    &   multicolumn{1}{P{110mm}|}{makecell{fontsize{18pt}{20pt}selectfonttextbf{Report}
                                           large  from a research center}}
            & multicolumn{2}{P{20mm}|}%
                          {makecell{Deliverable   2.0}}
                            & rule[-5mm]{0pt}{13mm}
    cline{2-5}
    &   multicolumn{2}{P{120mm}|}%
                    {makecell{Report title    and number}}
            &   Pagenewline
                thepage    & rule[-5mm]{0pt}{10mm}
    hline
    end{tabular}
}

usepackage{lipsum}
usepackage{hyperref} % had to be last in preamble

begin{document}

section{Section title}
thispagestyle{fancy}
subsection{Subsection title}
lipsumlipsum

end{document}

Answered by Zarko on September 24, 2020

Another variant, using multirowcell and taking into account that the number argument of multirow is not the number of rows, but the number of lines, which is not necessarily the same. In addition, your cells are unusual, since they include invisible rules to ensure a fixed height. So I put the multirow in the second row, with a negative, decimal number argument.

Unrelated: hyperref should be loaded as the last package, with very few exceptions.

documentclass[a4paper,10pt]{article}
usepackage{graphicx}
usepackage{hyperref}
usepackage{array}
usepackage{makecell}
usepackage{multirow}
usepackage{multicol}
usepackage{fancyhdr}
usepackage[hmargin=2cm,top=4cm,headheight=6cm,footskip=65pt]{geometry}
usepackage[version=4]{mhchem} % loads some package that allows to use "arrayrulewidth"
usepackage{lipsum}

% new columns definition
newcolumntype{M}[1]{@{}>{centeringarraybackslash}m{#1}@{}}
newcolumntype{N}{@{}m{0pt}@{}}

pagestyle{fancy}
renewcommandheadrulewidth{0pt} % no line between document and header
fancyhf{} % clear header and footer
fancyhead[C]{%
    begin{tabular}{|M{2.5cm-1.25arrayrulewidth}|M{11cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|M{1cm-1.25arrayrulewidth}|N}
        hline& makecell{Report  footnotesize{from a research center}} & multicolumn{2}{M{2cm-1.5arrayrulewidth}|}{makecell{Deliverable 2.0}} & rule{0pt}{1.3cm}
        cline{2-4}
        multirowcell{-4.8}{includegraphics[height=2.2cm,width=2.2cm]{example-image}}
        & multicolumn{2}{M{12cm-1.5arrayrulewidth}|}{makecell{Report title  and number}} & makecell{Page  thepage} & rule{0pt}{1cm}
        hline
    end{tabular}
}

begin{document}

section{Section title}
%% add this if you want the fancy style also on the first page of a chapter:
thispagestyle{fancy}
subsection{Subsection title}
lipsum[1-10]

end{document} 

enter image description here

Answered by Bernard on September 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP