Question
I am trying to get a table based on a multiple delimiter string. My current idea is to use \setsepchar{}
and \readlist
.
I put my requirements and details into a table because I would have a harder time putting the information into sentences in a way that makes sense.
Criteria
Answer Requirement
# | Requirement | Reason |
---|---|---|
1 | Optional add prefix to left column | Same left column prefix across all rows |
2 | Optional add suffix to right column | Same right column suffix across all rows |
3 | Function callable from tabular environment | |
4 | Usable within a node | The table is placed in a node in a tikzpicture environment |
Options
# | Option | Reason |
---|---|---|
1 | Multiple Delimiters | So long as the prefix and suffix condition is met multiple delimiters is not a necessity. |
2 | Environment Instead Of A Function | So long as the prefix and suffix condition is met using a function is not a necessity. |
Misc Details
# | Detail |
---|---|
1 | I prefer using xparse \NewDocumentCommand and \NewDocumentEnvironment due to ease with optional parameters |
2 | There will only ever be 2 columns, but the number of rows could be 20 or it could be 10. |
3 | I will be using this many times so having a function would be much easier than doing this by hand every single time. |
4 | Eventually this will be put into an ".sty" file. |
Code
\usepackage{xparse}
\usepackage{pgffor}
\usepackage{listofitems}
\makeatletter
% #1 = Prefix for left column
% #2 = Suffix for right column
% #3 = Left column header
% #4 = Right column header
% #5 = Input List
\NewDocumentCommand{\ptr}{O{} O{"} O{Parameter} O{Value} m}
{
#3 & #4 \\
\setsepchar{;/,}
\readlist\trlist{#5}
\foreach \n in {1,...,\trlistlen}
{
#1\trlist[\n,1] & \trlist[\n,2] #2 \\
\hline
}
\makeatother
\ProcessOptions\relax
\begin{document}
% As Examples
Data 1
\begin{tabular}{| l | r |}
\ptr[groupa00]["]{test1,1;test2,2;test3,3}
\end{tabular}
Data 2
\begin{tabular}{| l | r |}
\ptr[groupb00]["]{test1,1;test2,2;test3,3}
\end{tabular}
\end{document}
Output
The output should be the look like below:
Data 1
Parameter | Value |
---|---|
groupa00Test1 | 1" |
groupa00Test2 | 2" |
groupa00Test3 | 3" |
Data 2
Parameter | Value |
---|---|
groupb00Test1 | 1" |
groupb00Test2 | 2" |
groupb00Test3 | 3" |