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:
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.