6

I need to insert (floats) tables with a precisely defined style inside a technical document, and I succeeded in producing the sought for result by using the tabularx and the floatrow packages. An example code is reported below

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

% Preamble

% structure settings

\usepackage{amsmath}
\usepackage{array,color}
\usepackage{colortbl}  % Coloring of tabular material
\usepackage{calc}
\usepackage{floatrow,graphicx}
\usepackage{tabularx}  % Table design
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{siunitx}    % SI units symbols
\DeclareSIUnit{\AGA}{g}    % Average gravity acceleration symbol declaration
\newcommand{\vobjskip}{0.3\baselineskip}  % Vertical skip
\usepackage[justification=raggedright,font=bf]{caption}          % Captions "look and feel"
\captionsetup{justification=raggedright,singlelinecheck=false}   % Captions placement

% lipsum
\usepackage{lipsum}

%%%% Single table macro

\newcommand{\smpltab}%[2][plain]    % Basic table insertion macro
{\setlength{\intextsep}{\vobjskip}
  \floatsetup{style=plain,floatwidth=\textwidth, capposition=TOP, captionskip=-.05\baselineskip}  % Boxed style of figures
  \begin{table}[!h]\caption{Test caption}
    \begin{tabularx}{\linewidth}{|Y|Y|Y|}
      \hline
      \rowcolor[gray]{0.1} \textcolor{white}{Vibration direction} & \textcolor{white}{Speed~(\si[per-mode=symbol]{\m\per\s})} & \textcolor{white}{Acceleration~(\si[per-mode=symbol]{\AGA})}\\
      \hline
      \textit{x} axis & & \\
      \cline{1-1}
      \textit{y} axis & 0.0025 & 0.1 \\
      \cline{1-1}
      \textit{z} axis & & \\      
      \hline
    \end{tabularx}
  \end{table}}

\begin{document}
\lipsum[1]
\smpltab
\lipsum[2]
\lipsum[3]
\end{document}

and here it is the relevant snippet of graphical output:

enter image description here

After designing the tables with the required style, I tried to produce a float table producing environment with the following definition, in order to hide the detailed table structure in the input text file, while however preserving the freedom of adding as many row lines as needed:

\newenvironment{datatable}[1]
{\setlength{\intextsep}{\vobjskip}\floatsetup{style=plain,floatwidth=\textwidth, capposition=TOP, captionskip=-.05\baselineskip}  % Boxed style of figures
 \begin{table}[!h]\caption{#1}\begin{tabularx}{\linewidth}{|Y|Y|Y|}%
 \hline\rowcolor[gray]{0.1} \textcolor{white}{Vibration direction} & \textcolor{white}{Speed~(\si[per-mode=symbol]{\m\per\s})} & \textcolor{white}{Acceleration~(\si[per-mode=symbol]{\AGA})}\\
\hline}
{\hline%
\end{tabularx}%
\end{table}}

I placed in the body of the text the following call to this new environment, but then LaTeX broke down during processing:

\begin{datatable}{Test caption}
  \textit{x} axis & & \\
  \cline{1-1}
  \textit{y} axis & 0.0025 & 0.1 \\
  \cline{1-1}
  \textit{z} axis & & \\      
\end{datatable}

My questions therefore are the following:

  1. Is it possible to create such an environment producing floats with the packages I already use? And if yes how?
  2. Why the newenvironment command brokes down? I thought, macros were verbatim textual substitutions, but this seems to suggest that my though was false.
  • 1
    It's not really clear how you'll cope with the variable parts (caption, label). Are all the desired tables like the example one? – egreg Apr 8 '18 at 15:10
  • I have reported an incomplete example: I'll correct my question by adding a definition of an environment with parameters like one I've tried in my experiments. Moreover the style will be the same as the one shown in the example: when I'll need more columns, I'll define a new environment. – Daniele Tampieri Apr 8 '18 at 16:24
  • @egreg. Sincerely, I haven't thought about how to place a label. I thought of doing something like \begin{datatable}{Test caption}\label{tab:test}: is this wrong? – Daniele Tampieri Apr 8 '18 at 16:59
  • 1
    The problem AFAIK is tabularx here as it collects all content up to \end{tabularx} which is now hidden inside \end{datatable}. – Martin Scharrer Apr 8 '18 at 17:27
7
+50

I've always found floatrow too invasive so I removed it.

You have to use \table, \tabularx, \endtabularx and \endtable (see the documentation of tabularx).

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

% Preamble

% structure settings
\usepackage{amsmath}
\usepackage{array,color}
\usepackage{colortbl}  % Coloring of tabular material
\usepackage{tabularx}  % Table design
\usepackage{siunitx}   % SI units symbols
\usepackage[justification=raggedright,font=bf]{caption} % Captions "look and feel"

% lipsum
\usepackage{lipsum}

\newcolumntype{Y}{>{\centering\arraybackslash}X}
\DeclareSIUnit{\AGA}{g}    % Average gravity acceleration symbol declaration

\newcommand{\vobjskip}{0.3\baselineskip}  % Vertical skip
\captionsetup{justification=raggedright,singlelinecheck=false} % Captions placement

\newenvironment{datatable}[1]
 {%
  \setlength{\intextsep}{\vobjskip}%
  \table[!htp]
  \captionsetup{skip=0.3\baselineskip,position=top}
  \caption{#1}
  \tabularx{\linewidth}{|Y|Y|Y|}
  \hline
  \rowcolor[gray]{0.3}%
  \textcolor{white}{Vibration direction} &
  \textcolor{white}{Speed (\si[per-mode=symbol]{\m\per\s})} &
  \textcolor{white}{Acceleration (\si[per-mode=symbol]{\AGA})}\\
  \hline
 }
 {%
  \hline
  \endtabularx
  \endtable
 }

\begin{document}

\lipsum[1]

\begin{datatable}{Test caption\label{test}}
  $x$ axis & & \\
  \cline{1-1}
  $y$ axis & 0.0025 & 0.1 \\
  \cline{1-1}
  $z$ axis & & \\      
\end{datatable}

\lipsum[2]

\end{document}

enter image description here

  • Very nice solution! However I have to point out something I don't understand: where do you use the optional parameter? In the example I see the #2 parameter (the caption) but I only see the default value of the #1 parameter. – Daniele Tampieri Apr 9 '18 at 8:18
  • @DanieleTampieri Oh, yes, that's a vestige of the attempt with floatrow. I'll remove it. – egreg Apr 9 '18 at 10:02

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.