2

I'm having trouble recreating the following table in LaTeX: the table I want

(Note that in the first column, "Action" is centered while the rows beneath it are left aligned.) The closest I've gotten is this:

 \documentclass[12pt,oneside,letterpaper]{book}

  \usepackage{array}
  \usepackage{multirow}

  \begin{document}

 \begin{tabular}{|l|
 >{\centering\arraybackslash}p{0.75in}|  % this centers over multiple lines
 >{\centering\arraybackslash}p{0.75in}|
 >{\centering\arraybackslash}p{1.1in}|
 >{\centering\arraybackslash}p{1.1in}|
 >{\centering\arraybackslash}p{1.1in}|}
 \hline 
 \multicolumn{1}{|c|}{Action} % Fake multicol centers action
 & \multicolumn{2}{|c|}{Magnetic Flux}  % Merge two columns
 & \multirow{2}{1.1in}{\centering Direction of \\ Induced $B$ \\ Field  (Expected)} % Multirow seems not to be working?
 & \multirow{2}{1.1in}{\centering Expected Direction of Induced Current (CW/CCW)}
 & \multirow{2}{1.1in}{\centering Actual Direction of Induced Current } \\ 
 \hline 
 & Direction (Up/Down) & Increasing/ Decreasing &&& \\ % This row isn't behaving right
 \hline
 N pole in & & & & & \\ \hline 
 N pole out & & & & & \\ \hline
 S pole in & & & & & \\ \hline 
 S pole out & & & & & \\ \hline
 \end{tabular} 
 \end{document}

which produces this table: the table i have

It seems like the multirow isn't playing nice with the way I have things extending over multiple columns? And because I'm using a "fake" multicol to center action I'm having trouble getting it to extend over multiple rows.

Any help would be much appreciated! Thanks!

EDIT: I am running on Mac and compiling with pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019), and YES I do get the table shown. If people get a different table can they show what they get, with what compiler? I have added the \begin{document} and \end{document} along with the document class. This is the full file that I am compiling.

Links to .tex and resulting .pdf: https://www.dropbox.com/s/2o92882h9q4xtt0/test.pdf?dl=0 https://www.dropbox.com/s/ho1a5mzbhjoe5oi/test.tex?dl=0

6
  • The code you posted doesn't have this problem for me. Do you really have it with this very code?
    – Bernard
    Aug 22, 2019 at 1:01
  • @Bernard The code produced the bottom table for me- "Action" and the cell beneath it are not merged, and the three columns on the right have lines passing through the cell and are missing dividing lines between them. (This is the very code and a screen shot of its output.) What does it look like for you? Aug 22, 2019 at 1:17
  • @LaurenPearce, showed image is not produced with your code. Table has all vertical lines (also those which in your image are missed).
    – Zarko
    Aug 22, 2019 at 1:45
  • @Zarko what compiler are you using? What system? This is what I get when I run it. Aug 22, 2019 at 1:47
  • @Zarko can you upload an image of what you get? I've added the \begin{document} and \end{document} along with the document class; this is the full file that I am compiling and that gives the image. Aug 22, 2019 at 1:53

3 Answers 3

3

I suppose, that you looking for the following:

enter image description here

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{array, makecell, multirow}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\setcellgapes{3pt}
\makegapedcells

\begin{document}
 \begin{tabular}{|l| P{0.75in}|  
                     P{0.75in}|
                     P{1 in}|
                     P{1.2 in}|
                     P{1 in}|}
    \hline
\multirow{4}{*}{Action}
    & \multicolumn{2}{c|}{Magnetic Flux}    &   &   &   \\
    \cline{2-3}
    & \makecell{Direction\\ (Up/Down)}
        & \makecell{Increasing/\\ Decreasing}
            & \multirow{-2.8}{=}{\centering Direction of Induced $B$ Field  (Expected)}
                & \multirow{-2.8}{=}{\centering Expected Direction of Induced Current (CW/CCW)}
                    & \multirow{-2.8}{=}{\centering Actual Direction of Induced Current}   \\
 \hline
 N pole in  & & & & & \\ \hline
 N pole out & & & & & \\ \hline
 S pole in  & & & & & \\ \hline
 S pole out & & & & & \\ \hline
 \end{tabular}
 \end{document}
10
  • Thanks! This is close, but "Action" isn't centered in this one. It looks like if I wrap it in a fake multicolumn then it's centered, though: \multicolumn{1}{|c|}{\multirow{4}{*}{Action} } EDIT: Ah you changed the post while I wrote this. Aug 22, 2019 at 2:35
  • @LaurenPearce, see edited answer. "Action is pretty well vertical centered and perfectly horizontal. Adding \multicolumn is just a clutter in code, however, if you like it, please add it in your code. (BTW, instead of thanks" you can upvote my answer :-) ).
    – Zarko
    Aug 22, 2019 at 2:40
  • I would upvote your answer but the revised one doesn't work- the text hangs out of the lines. (I"m putting a copy on dropbox now so you can see.) Your first one worked well for me (once I changed action as mentioned above) but this one doesn't produce the table you have at the top of your post (at least on my machine)- the text of the later three columns is running off the top Aug 22, 2019 at 2:41
  • Here's the difference between your original and editing solutions when I run on my machine: dropbox.com/s/andb0qxlj6sg913/zarko.pdf?dl=0 Aug 22, 2019 at 2:43
  • @LaurenPearce, ups, I forgot to add two lines of code in MWE preamble. Now is corrected and tested.
    – Zarko
    Aug 22, 2019 at 2:44
0

I enclosed the code for two calstables, typeset with the help of the package cals.

The first table is typeset with vertical rules. Due to the width of the headers, I reduced margins with the help of geometry.

I have aligned the second row headers at the bottom of the cells, the content of all other cells are centred vertically. Also, the font size used in the header is reduced, making the them less dominant and disturbing.

The second calstable is typeset without vertical rules. As yo will find, those are unnecessary for the readability.

enter image description here

\documentclass[12pt,oneside,letterpaper]{book}
\usepackage{lmodern, cals}
\usepackage[left=3cm, right=3cm]{geometry}

% Set up the carlstable
\makeatletter
%% All rules of same weight
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}
\def\cals@AtBeginCell{\vfil}            % All cell contents vertically centred

% Shorthands for spanning cells
\let\nc=\nullcell
\let\sc=\spancontent

\def\rb{\ifx\cals@borderR\relax     % Right Border (rules) switch (off-on)
    \def\cals@borderR{0.0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left Border switch (off-on)
    \def\cals@borderL{0.0pt}
\else \let\cals@borderL\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Bottom Border switch (off-on)
    \def\cals@borderB{0.0pt}
\else \let\cals@borderB\relax\fi}

\def\lp{\ifdim\cals@paddingL=0.0pt\relax    % Left padding switch (off-on) 
\setlength{\cals@paddingL}{3pt}
\else \setlength{\cals@paddingL}{0pt}\fi}

\def\rp{\ifdim\cals@paddingR=0.0pt\relax    % Right padding switch (off-on)
\setlength{\cals@paddingR}{3pt}
\else \setlength{\cals@paddingR}{0.0pt}\fi}
\makeatother

\begin{document}

\begin{calstable}[c]

% Defining 5 columns, 1 are for double vertical rules (col2)
\colwidths{{\dimexpr(\columnwidth/50*7+8pt)}
                   {\dimexpr(\columnwidth/50*7-5pt)}
                   {\dimexpr(\columnwidth/50*7-5pt)}
                   {\dimexpr(\columnwidth/50*9)}
                   {\dimexpr(\columnwidth/50*10+2pt)}
                   {\dimexpr(\columnwidth/50*9)}
            }
\makeatletter
\setlength{\cals@paddingL}{2pt}    % Changes to padding has to go inside the tables preamble
\setlength{\cals@paddingR}{2pt}   % Decrease tabcolsep from 4.96pt to 2pt
\setlength{\cals@paddingB}{3pt} % Uncomment if you want less space between cells
%\setlength{\cals@paddingT}{2pt}

% R1H1 
\thead{\footnotesize%
\brow
    \nc{lrt}
    \nc{ltb}
    \nc{rtb}\alignC\sc{Magnetic Flux}
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
\erow
%
%R2H2 % Header
\brow
    \nc{lrb}\alignC\sc{Action}
    \cell{\vfill Direction\\ (Up/Down)}
    \cell{\vfill Increasing/\\ Decreasing}
    \nc{lrb}\alignC\sc{\vfill Direction of\\Induced $B$\\\mbox{Field (Expected)}}
    \nc{lrb}\alignC\sc{\vfill \mbox{Expected Direction}\\\mbox{of Induced Current}\\(CW/CCW)}
    \nc{lrb}\alignC\sc{\vfill\mbox{Actual Direction}\\of Induced\\Current\vphantom{j}} % \vphantom command since Current has no depth
\erow
}
\tfoot{\lastrule\strut}
\small
%R3B1
\brow
    \alignL\cell{N pole in}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%R4B2
\brow
    \alignL\cell{N pole out}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%R5B3
\brow
    \alignL\cell{S pole in}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%R6B4
\brow
    \alignL\cell{S pole out}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
\makeatletter
\end{calstable}\par

\vspace{1cm}

\begin{calstable}[c]

% Defining 5 columns, 1 are for double vertical rules (col2)
\colwidths{{\dimexpr(\columnwidth/50*7+8pt)}
                   {\dimexpr(\columnwidth/50*7-5pt)}
                   {\dimexpr(\columnwidth/50*7-5pt)}
                   {\dimexpr(\columnwidth/50*9)}
                   {\dimexpr(\columnwidth/50*10+2pt)}
                   {\dimexpr(\columnwidth/50*9)}
            }
\makeatletter
%% All rules of same weight
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.0pt}
\def\cals@bodyrs@width{0.6pt}
\def\cals@cs@width{0.0pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.3pt}
\def\cals@AtBeginCell{\vfil}            % All cell contents vertically centred
\setlength{\cals@paddingL}{3pt}    % Changes to padding has to go inside the tables preamble
\setlength{\cals@paddingR}{3pt}   % Decrease tabcolsep from 4.96pt to 2pt
\setlength{\cals@paddingB}{3pt} % Uncomment if you want less space between cells
%\setlength{\cals@paddingT}{2pt}

% R1H1 
\thead{\footnotesize%
\brow
    \lp\nc{lrt}\lp
    \nc{ltb}
    \nc{rtb}\alignC\sc{Magnetic Flux}
    \nc{lrt}
    \nc{lrt}
    \rp\nc{lrt}\rp
\erow
%
%R2H2 % Header
\brow
    \lp\nc{lrb}\alignC\sc{Action}\lp
    \cell{\vfill Direction\\ (Up/Down)}
    \cell{\vfill Increasing/\\ Decreasing}
    \nc{lrb}\alignC\sc{\vfill Direction of\\Induced $B$\\\mbox{Field (Expected)}}
    \nc{lrb}\alignC\sc{\vfill \mbox{Expected Direction}\\\mbox{of Induced Current}\\(CW/CCW)}
    \rp\nc{lrb}\alignC\sc{\vfill\mbox{Actual Direction}\\of Induced\\Current\vphantom{j}}\rp % \vphantom command since Current has no depth
\erow
}
\tfoot{\lastrule\strut}
\small
%R3B1
\brow
    \lp\bb\alignL\cell{N pole in}\lp
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \rp\cell{}\rp
\erow
%R4B2
\brow
    \lp\alignL\cell{N pole out}\lp
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \rp\cell{}\rp
\erow
%R5B3
\brow
    \lp\alignL\cell{S pole in}\lp
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \rp\cell{}\bb\rp
\erow
%R6B4
\brow
    \lp\alignL\cell{S pole out}\lp
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \rp\cell{}\rp
\erow
\makeatletter
\end{calstable}\par
\end{document}

\end{document}
0

You can do easily that tabular with {NiceTabular} of nicematrix.

In {NiceTabular}, you merge cells both horizontally and vertically with the command \Block. For the rows, you give the number of logical rows (and not the number of physical lines as with \multicolumn).

With the key hvlines, all the rules are drawn, excepted in the blocks (constructed by \Block).

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{lccccc}[hvlines,cell-space-limits=3pt]
\Block[c]{2-1}{Action} 
  & \Block{1-2}{Magnetic Flux} 
    & 
      & \Block{2-1}{Direction of\\ Induced $B$ Field\\ (Expected)}
        & \Block{2-1}{Expected Direction\\ of Induced Current\\ (CW/CCW)}
          & \Block{2-1}{Actual\\ Direction of\\ Induced Current} \\
& \Block{}{Direction\\ (Up/Down)} & \Block{}{Increasing/\\ Decreasing} \\
N pole in  \\ 
N pole out \\ 
S pole in  \\ 
S pole out \\ 
\end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .