2

I have the following code:

\documentclass[11pt]{book} \usepackage[default]{opensans} \usepackage{alltt} \usepackage{amsfonts} \usepackage{amsmath} \usepackage{amssymb} \usepackage{array} \usepackage{colortbl} \usepackage{enumitem} \usepackage{fancyhdr} \usepackage{float} \usepackage[total={15.24cm,22.86cm}]{geometry} \usepackage{graphicx} \usepackage{hyperref} \usepackage{inconsolata} \usepackage{listings} % % % Package used for breakable tables \usepackage{longtable} % % \usepackage{mathabx} \usepackage{microtype} % % For creating new commands with an arbitrary number of arguments \usepackage{newfile} % % \usepackage{pgfplots} \usepackage{pifont} \usepackage{smartdiagram} \usepackage{tabularx} \usepackage[listings,skins,most]{tcolorbox} \usepackage{tikz} \usepackage[table,xcdraw]{xcolor} % % % Define custom colors for the header row \definecolor{headercolor}{RGB}{27, 29, 31} % % % % % Define a header style command \newcommand{\headerstyle}[1]{\textcolor{white}{\textbf{#1}}} % % % % Define the printDbBreakableTable environment \newenvironment{printDbBreakableTable}[3]{% % Begin table structure by indicating the number of columns \begin{longtable}{#1} % % Set header row color \rowcolor{headercolor!85}% % % The content of the header row #2% \endhead % % Insert the table's main content #3% \end{longtable}% } \begin{document} \begin{printDbBreakableTable} { |c|c|c|c| } {% \headerstyle{Employee\_id} & \headerstyle{First\_name} & \headerstyle{Last\_name} & \headerstyle{Salary} \\\hline} {% id-0001 & fname-01 & lname-01 & 100000 \\\hline id-0002 & fname-02 & lname-02 & 100001 \\\hline id-0003 & fname-03 & lname-03 & 100002 \\\hline id-0004 & fname-04 & lname-04 & 100003 \\\hline id-0005 & fname-05 & lname-05 & 100004 \\\hline id-0006 & fname-06 & lname-06 & 100005 \\\hline id-0007 & fname-07 & lname-07 & 100006 \\\hline id-0008 & fname-08 & lname-08 & 100007 \\\hline id-0009 & fname-09 & lname-09 & 100008 \\\hline id-0010 & fname-10 & lname-10 & 100009 \\\hline id-0011 & fname-11 & lname-11 & 100010 \\\hline } \end{printDbBreakableTable} \end{document} 

Is there any way to make the content inside the cells of the table really 100% exactly vertically aligned in the middle? So far I have tested

  • |m|m|m|m|
  • And also:
{\centering\arraybackslash}m{.15\linewidth}|% {\centering\arraybackslash}m{.15\linewidth}|% {\centering\arraybackslash}m{.15\linewidth}|% {\centering\arraybackslash}m{.15\linewidth}|% 

But none of these really put the text vertically in the middle.

I'm really surprised how challenging this task is in LaTeX compared to tools such as Microsoft Excel and Libre Office Calc.

Any idea about how to solve this issue?

My requirement : The content in the cells should be 100% in the middle, both horizontally and vertically.

Thanks in advance.

5
  • 1
    Welcome to TeX.SE. Try running \addtolength\extrarowheight{3.5pt} immediately before \begin{printDbBreakableTable}. Commented Sep 29, 2024 at 18:44
  • 1
    @Mico, your suggestion works fine until in some cell content haven't letters g, j , p, q ... We don't know what table content is actually is. I think that text in table is correctly vertically centered. Commented Sep 29, 2024 at 18:51
  • @Zarko You mean the position of different letters, compared to the baseline? Indeed, I just did a test with values including g, p, q, etc. and it doesn't working. So there is no general 100% workaround for this problem, right? Commented Sep 29, 2024 at 18:54
  • @Zarko - Please see my answer for a better fix. :-) Commented Sep 29, 2024 at 19:02
  • 1
    @Mico, your answer is the way to go (+1), however with constraints which I try to show in mine ... and of course, to remove horizontal lines eliminate "anchors" by which reader can easily observe, if text in cells are vertical centered. Commented Sep 29, 2024 at 19:07

2 Answers 2

3

A direct fix, at least for the sample table at hand, is to run

\addtolength\extrarowheight{3.5pt} 

immediately before \begin{printDbBreakableTable}.

There are actually two deeper problems with this table. First, why does it have so many vertical rules? The optimal number of vertical rules for most tables, aesthetically speaking, is zero. Omitting all vertical rules immediately gives the table a much more open and inviting look. Second, what's up with all those horizontal rules? They aren't needed either. Moreover, if it weren't for their presence, it would never occur to you or anyone else to think about wanting to vertically center the cell contents.

In short, do yourself -- and, even more importantly, your readers -- a big favor and omit all vertical rules and almost all horizontal rules.

enter image description here

\documentclass[11pt]{book} \usepackage{iftex} \ifpdftex \usepackage[T1]{fontenc} \fi % new \usepackage{booktabs} % new, for '\bottomrule' macro \usepackage[default]{opensans} \usepackage{alltt} %%\usepackage{amsfonts} % 'amsfonts' is loaded automatically by 'amssymb' \usepackage{amsmath} \usepackage{amssymb} \usepackage{array} \usepackage{colortbl} \usepackage{enumitem} \usepackage{fancyhdr} \usepackage{float} \usepackage[total={15.24cm,22.86cm}]{geometry} \usepackage{graphicx} \usepackage{hyperref} \usepackage{inconsolata} \usepackage{listings} % % % Package used for breakable tables \usepackage{longtable} % \usepackage{mathabx} \usepackage{microtype} % % For creating new commands with an arbitrary number of arguments \usepackage{newfile} % % \usepackage{pgfplots} \usepackage{pifont} \usepackage{smartdiagram} \usepackage{tabularx} \usepackage[listings,skins,most]{tcolorbox} \usepackage{tikz} \usepackage[table,xcdraw]{xcolor} % % % Define custom colors for the header row \definecolor{headercolor}{RGB}{27, 29, 31} % % % Define a header style command \newcommand{\headerstyle}[1]{\textcolor{white}{\textbf{#1}}} % % Define the printDbBreakableTable environment \newenvironment{printDbBreakableTable}[3]{% % Begin table structure by indicating the number of columns \begin{longtable}{#1} % % Set header row color \rowcolor{headercolor!85}% % % The content of the header row #2% \endhead % % Insert the table's main content #3% \end{longtable}% } \begin{document} \addtolength\extrarowheight{2pt} % optional, mainly for header row \begin{printDbBreakableTable} {llll} {% \headerstyle{Employee\_id} & \headerstyle{First\_name} & \headerstyle{Last\_name} & \headerstyle{Salary} \\} {% id-0001 & fname-01 & lname-01 & 100000 \\ id-0002 & fname-02 & lname-02 & 100001 \\ id-0003 & fname-03 & lname-03 & 100002 \\ id-0004 & fname-04 & lname-04 & 100003 \\ id-0005 & fname-05 & lname-05 & 100004 \\ id-0006 & fname-06 & lname-06 & 100005 \\ id-0007 & fname-07 & lname-07 & 100006 \\ id-0008 & fname-08 & lname-08 & 100007 \\ id-0009 & fname-09 & lname-09 & 100008 \\ id-0010 & fname-10 & lname-10 & 100009 \\ id-0011 & fname-11 & lname-11 & 100010 \\ \bottomrule % slightly thicker than "\hline" } \end{printDbBreakableTable} \end{document} 
5
  • Well, I'm afraid this (I mean the idea) is going to be used inside a book about SQL written via LaTeX/Tikz. Therefore, a relational database table without vertical rules, doesn't make sense, it's not nice at all. Commented Sep 29, 2024 at 19:05
  • @user17911 - Did you give it a try? Commented Sep 29, 2024 at 19:06
  • 1
    Yes, I tried. IMHO, there is absolutely no flavor of what can be called "tabular" by drawing the table this way, particularly if there are rather big numbers of rows (at least > 10 rows). It's like the text is flying in the air. Of course, this is just my personal point of view and taste. Obviously, many would prefer otherwise. Even when I use Microsoft Excel or LibreOffice Calc, I always put borders everywhere on all cells. For me, no border = no table! :):) Commented Sep 30, 2024 at 18:51
  • @user17911 - If keeping the vertical and horizontal rules is essential, as you maintain, then do go the route of setting the length parameter \extrarowheight to a value of between 2pt and 4pt. The optimal value will depend crucially on what exactly is in the table. (Note that I set \addtolength\extrarowheight{2pt} in the answer above -- mainly to help with the lessen the visual imbalance that would otherwise be present in the header row.) Commented Sep 30, 2024 at 19:23
  • 1
    It seems that this gives the best result (although not for letters such as p, q, etc.) but in general, I think is the closest think we can obtain (or at least me as a rather beginner) in LaTeX. Many thanks for your time, help and patience. I just accepted your solution with \addtolength\extrarowheight{2pt} as the answer. Commented Sep 30, 2024 at 19:59
2

Just for illustrate, what I mean in my comments. For simplify MWE i use for table tblr od tabularray package (problem is the same with you code):

\documentclass[11pt]{book} \usepackage[total={15.24cm,22.86cm}]{geometry} \usepackage[default]{opensans} \usepackage{alltt} \usepackage{microtype} \usepackage[xcdraw]{xcolor} \usepackage{tabularray} \begin{document} \begin{tblr}{hlines, vlines, colspec={*{4}{Q[c,m]}}, row{1} = {bg=black, fg=white, font=\bfseries} } % column headers Employee\_id & First\_name & Last\_name & Salary \\ % table body id-0001 & fname-01 & lname-01 & 100000 \\ id-0002 & fname-02 & lname-02 & 100001 \\ id-0003 & fname-03 & lname-03 & 100002 \\ id-0004 & gjhij-04 & pqrst-04 & 100003 \\ id-0005 & gjhij-05 & pqrst-05 & 100003 \\ id-0006 & gjhij-06 & pqrst-06 & 100003 \\ \end{tblr} \end{document} 

enter image description here

1
  • +1 as well. :-) I think that as long as there's at least one column, such as the "Salary" column in the table at hand, which consists exclusively of arabic numerals, increasing the value of \extrarowheight by at least 2pt is probably a good idea to lessen the vertical whitespace imbalance. Commented Sep 29, 2024 at 19:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.