1

This is my first post here so please understand if I did something wrong. I want to have a table in LaTeX where icons should be displayed in the first column (centered) and text (vertically centered) in the second row.

I want to insert the whole thing as \newcommand. My problem is shown in the following picture. With one line of text everything looks centered. But if there are more lines of text, the icon stays at its position. The icon is centered horizontally, but not vertically. Can anyone help me?

Best regards

Michael

\documentclass{article}
\usepackage[]{graphicx}

\newcommand{\icon}[2][1cm]{\begin{minipage}{#1}\includegraphics[width=#1]{#2}\end{minipage}}

\begin{document}

\begin{tabular}{|c|p{13cm}|}\hline 
Symbol & Description \\\hline
 & \\ [-0.5em] \icon{icon_settings.pdf} & Text in only one line. \\ [-0.5em] & \\\hline
 & \\ [-0.5em] \icon{icon_administrator.pdf} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. \\ [-0.5em] & \\\hline
\end{tabular}

\end{document}

enter image description here

  • welcome to tex.se! see tex.stackexchange.com/questions/449556 (your question is duplicate to this and many similar) – Zarko Feb 13 at 8:43
  • Thanks for the answer. I tried this code, but it's the same problem. The first line is in the middle of the image, but if the text is longer than one line, then there is the problem. – Michael Feb 13 at 9:00
  • see my answer below. in it i adopt mentioned answer to your case. – Zarko Feb 13 at 9:11
1

i would do the following:

  • use tabularx for table environment
  • for the second column use \renewcommand\tabularxcolumn[1]{m{#1}}, which vertically center cells contents
  • include images in table with macro \adjustimage from the package {adjustbox} move image baseline to its vertical center
  • with \adjustboxset define image size, vertical position and its margins:
  • with the package cellspace add some vertical space above and below cells with texts

    \documentclass{article}
    \usepackage{adjustbox}% [demo]
    \usepackage{cellspace, tabularx}
    \setlength\cellspacetoplimit{3pt}
    \setlength\cellspacebottomlimit{3pt}
    \addparagraphcolumntypes{X}
    
    %---------------- show page layout. don't use in a real document!
    \usepackage{showframe}
    \renewcommand\ShowFrameLinethickness{0.15pt}
    \renewcommand*\ShowFrameColor{\color{red}}
    %---------------------------------------------------------------%
    \begin{document}
        \begin{table}
    \renewcommand\tabularxcolumn[1]{m{#1}}
    \adjustboxset{width=.1\textwidth,valign=c, margin=0pt 3pt 0pt 3pt}
    \begin{tabularx}{\linewidth}{|c|X|}
        \hline
    Symbol & Description    \\
        \hline
    \adjustimage{}{example-image-duck} & Text in only one line.     \\
        \hline
    \adjustimage{}{example-image-duck} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line.         \\
        \hline
    \end{tabularx}
        \end{table}
    \end{document}
    

enter image description here

(red lines indicate text borders=

if you like to change size of the particular image, than include width of this image for example as follows:

\adjustimage{width=2cm}{example-image-duck}

enter image description here

edit: you also can stick with your solution. in preamble just add array package and instead of p{...} column type use m{...}:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{array}

\newcommand{\icon}[2][1cm]{\begin{minipage}{#1}\includegraphics[width=#1]{#2}\end{minipage}}

\begin{document}

\begin{tabular}{|c|m{13cm}|}\hline
Symbol & Description \\\hline
 & \\ [-0.5em] \icon{icon_settings.pdf} & Text in only one line. \\ [-0.5em] & \\\hline
 & \\ [-0.5em] \icon{icon_administrator.pdf} & Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. Here is a longer text that is displayed over more than one line. The symbol is not vertically centered, but has the same distance to the top as the icon in the first line. \\ [-0.5em] & \\\hline
\end{tabular}

\end{document}

enter image description here

  • Thank you very much for your help and the detailed description. With the small change I could do a lot and solve my problem. – Michael Feb 13 at 12:15

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.