2

In constructing a table in LaTeX, I am exporting data from Excel with a hierarchical representation, but for more optimal printing I need to collapse the first four columns and replace the empty cells to the left with indentations.

For example, the structure in Excel:

| Category | Subcategory | Type | Name | Code | Variable | Note      |
|----------|-------------|------|------|------|----------|-----------|
| A        |             |      |      |      |          |           |
|          | A           | A    | One  | one  | one      | Something |
|          | B           | A    | One  | One  | One      | Something |
|          | C           | A    | One  | One  | One      | Something |
| B        |             |      |      |      |          |           |
|          | A           | A    | One  | One  | One      | Something |
|          |             | B    | One  | One  | One      | Something |
|          | B           | A    | One  | One  | One      | Something |
|          |             | B    | One  | One  | One      | Something |

(Edited to better represent the hierarchy)

To achieve this I followed the example here Adding vertical hierarchy lines to a category column of a table, but this involved a lot of manual editing of the Latex table-code (which I generated from TablesGenerator) - thus making adding new contents to the table very tiresome.

This looks like the following:

MWE

Can the pst-node-package, or some other package, ease the process of this column-collapse?

That is, without the manual editing of the table to create the indentation. The pst-node-package code used in the answer to the linked question is a bit hard to fathom, but the result looks somewhat close to the MWE output above.

MWE:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}

\usepackage[pass]{geometry}
\usepackage{underscore}
\usepackage{booktabs}
\usepackage{lscape}
\usepackage{longtable}
\newcommand{\lvl}{~~~}
\usepackage{array}

\begin{document}

\newgeometry{margin=0.25cm}
\begin{longtable}{p{5.5cm}p{6cm}p{2.5cm}p{4cm}}
\toprule
\textbf{Name} & \textbf{Code} & \textbf{Variable} & \textbf{Note} \\
\midrule
Cat A & A & a &  \\
\lvl SubCat A & A_A1 & a_a1_all & Note \\
\lvl\lvl Type A & A_A1_A & a_a1 &  \\
\lvl\lvl\lvl Item A & A_A1_A_A & a_a1_a &  \\
\lvl\lvl\lvl Item B & A_A1_A_B & a_a1_b &  \\
\lvl SubCat B & A_B1 & a_b1_all & Note \\
\lvl\lvl Type B & A_B1_B & a_b1 &  \\
\lvl\lvl\lvl Item A & A_B1_A & a_b1_a &  \\
\lvl\lvl\lvl Item B & A_B1_B_B & b_a1_b &  \\
\bottomrule
\end{longtable}
\restoregeometry

\end{document}

Solution

As it turned out in my case, given an initial table in Excel, the easiest way of doing this was in Excel itself. See Collapse columns horizontally in Excel on Stackoverflow for the solution.

  • What you would like to have is not quite clear to me. Could you post a (hand-)sketch of what the result should look like? – Bernard Jan 5 '16 at 11:15
  • I composed a MWE and added it. – OleVik Jan 5 '16 at 11:45
2
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{pmboxdraw}
\usepackage{xcolor}
\def\SFii{\textcolor{red}{\textSFii\textSFx}}
\def\SFviii{\textcolor{red}{\textSFviii\textSFx}}
\def\SFx{\textcolor{red}{\textSFx}}
\def\SFxi{\textcolor{red}{\textSFxi}}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{booktabs}    
\begin{document}

\begin{tabular}{*3{>{\ttfamily}l} l}\toprule
\textrm{\emph{Name}} & \textrm{\emph{Code}} & \textrm{\emph{Variable}} & \emph{Note} 
\\\midrule
Cat A      & & & \\
\SFviii SubCat A       & A\_A1       & a\_a1\_all & Note \\
\SFxi~\SFii Type A     & A\_A1\_A    & a\_a1      &      \\
\SFxi~~~\SFviii Item A & A\_A1\_A\_A & a\_a1\_a   &      \\
\SFxi~~~\SFii Item B   & A\_A1\_A\_B & a\_a1\_b   &      \\
\SFii SubCat B         & A\_B1       & a\_b1\_all & Note \\
~~\SFii Type B         & A\_B1\_B    & a\_b1      &      \\
~~~~\SFviii Item A     & A\_B1\_A\_A & a\_b1\_a   &      \\
~~~~\SFii Item B       & A\_B1\_B\_B & b\_a1\_b   &      \\\bottomrule
\end{tabular}

\end{document} 

enter image description here

With package newunicodechar you can define the characters and use directly if they are present with your keyboard:

%\usepackage{newunicodechar}
%\newunicodechar{└}{\textSFii}
%\newunicodechar{├}{\textSFviii}
%\newunicodechar{─}{\textSFx}
%\newunicodechar{|}{\textSFxi}
  • Also a very good example of an implementation of the example-image I linked to initially, but as I had written below it what I am trying to solve is not the adding of lines between levels but rather collapsing the columns and adding indentation automatically - ie, not manually. I edited the question clarify this. – OleVik Jan 5 '16 at 18:46
  • I accepted this as the answer because it was closest to what I was trying to achieve. However, in the end I did this in Excel, see Collapse columns horizontally in Excel for a method. – OleVik Jan 9 '16 at 16:58
2

Here is a solution with pst-node. The \psDefBoxNodes{name}{text} puts text in a box and defines 12 associated node: 3 on top , 3 on the centre line, 3 on the base line and 3 on the bottom. The names of the nodes are name:tl, ame:tC, name:tr, &c. Then the relevant nodes are connected with a custom \tconnect command, which is a \ncangle command with suitable parameters:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}

\usepackage[pass]{geometry}
\usepackage{underscore}
\usepackage{booktabs}
\usepackage{lscape}
\usepackage{longtable}
\newcommand{\lvl}{\hskip1.667em}
\usepackage{array}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\newcommand\tconnect[2]{\psset{angleA=-90, angleB=180, offsetA =4pt,%
offsetB=2pt}\ncangle{#1:bl}{#2:Bl}}

\begin{document}

\newgeometry{margin=0.25cm}
\begin{postscript}\psset{linecolor=red, linewidth=0.5pt, nodesepA=1ex, nodesepB =0.5ex, linejoin=1}
  \renewcommand\arraystretch{1.5}
  \begin{tabular}{p{5.5cm}p{6cm}p{2.5cm}p{4cm}}
    \toprule
    \textbf{Name} & \textbf{Code} & \textbf{Variable} & \textbf{Note} \\
    \midrule
    \psDefBoxNodes{cA}{Cat A} & & & \\
    \lvl \psDefBoxNodes{scA}{SubCat A} & A_A1 & a_a1_all & Note \\
    \lvl\lvl \psDefBoxNodes{tA}{Type A} & A_A1_A & a_a1 & \\
    \lvl\lvl\lvl \psDefBoxNodes{iAA}{Item A} & A_A1_A_A & a_a1_a & \\
    \lvl\lvl\lvl \psDefBoxNodes{iAB}{Item B} & A_A1_A_B & a_a1_b & \\
    \lvl \psDefBoxNodes{scB}{SubCat B} & A_B1 & a_b1_all & Note \\
    \lvl\lvl \psDefBoxNodes{tB}{Type B} & A_B1_B & a_b1 & \\
    \lvl\lvl\lvl \psDefBoxNodes{iBA}{Item A} & A_B1_A & a_b1_a & \\
    \lvl\lvl\lvl \psDefBoxNodes{iBB}{Item B} & A_B1_B_B & b_a1_b & \\
    \bottomrule
  \end{tabular}
  \psset{angleA=-90, angleB=180, offsetA =4pt, offsetB =2pt}
  \tconnect{cA}{scA}
  \tconnect{cA}{scB}
  \tconnect{scA}{tA}
  \tconnect{scB}{tB}
  \tconnect{tA}{iAA}
  \tconnect{tA}{iAB}
  \tconnect{tB}{iBA}
  \tconnect{tB}{iBB}

\end{postscript}
\restoregeometry

\end{document} 

Note: I don't think this code could break across pages, so I replaced the longtable environment with a plain tabular one.

enter image description here

  • This very excellently creates an output like the example-image I linked to initially, but I think my explanation was a bit lacking. I replace that image with the output from the MWE, which is a better representation of it. I am still having the problem of how to create the indentation/collapsing the columns without manually editing the table though. – OleVik Jan 5 '16 at 18:44

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.