3

I'm getting a little bit lost with the following. As I'm quite new to using TeX for more than just typesetting, I hope to get some help here.

I already searched this forum for some helpful posts and found many of them, but I didn't got all problems solved I ran into.

What I want to do is to automatically typeset a list of IP addresses and the corresponding FQDNs into a table. It should look like:

Source: IP1:FQDN1;IP2:FQDN2;...

       |  IP1   FQDN1
Hosts  |  IP2   FQDN2
       |  ...   ...

Here's what I tried to do (shortened down the code to the essential piece):

\documentclass{article}
\usepackage{xstring}
\usepackage{multirow}

\begin{document}
% test input
\newcommand{\myinput}{IP1:FQDN1;IP2:FQDN2}

\newcommand{\splitstring}[1]{
  % split input into table rows
  \ifx\@empty#1\else
  \saveexpandmode\expandarg
  % split column elements with leading & (after multirow)
  \StrSubstitute{\tmp}{;}{\empty\\&}[\tmp] % fails with: forbidden control sequence found while scanning use of \@xs@next
  % split row elements
  \StrSubstitute{#1}{:}{&}[\tmp]
  \restoreexpandmode\tmp
}

\begin{tabular}{l|ll}
  % StrCount fails (Missing number, treated as zero)
  \multirow{\StrCount{\myinput}{:}}{*}{Hosts} & \splitstring{\myinput}
\end{tabular}
\end{document}

Edit

Some additional information: Changing the line

 \StrSubstitute{\tmp}{;}{\empty\\&}[\tmp]

to

 \StrSubstitute{\tmp}{;}{\empty\\}[\tmp]

(and removing the &) makes this step build without errors. The output looks like this:

        |  IP1   FQDN1 
 Hosts  |
  IP2   |  FQDN2

As you can see the & is needed for correct alignement in the table. I'm wondering why the substitution of ":" by "&" works without error, but this one fails.

No further news on feeding the StrCount output into the multirow-environment.

Edit

I'm not sure if I can use the output from the xstring-functions for typesetting or if they can be converted to be used as parameters.

Any help is greatly appreciated.

  • Is your input always IP:FQDN; type? The reason why I'm asking is to understand whether I can use ; as the row separator and : as the column sparator for a possible pgfplotstable answer. – percusse Aug 7 '13 at 14:00
  • Yes, ; is the row sep and : is the column sep. Already thought about doing it the pfg/tikz way. But got stuck in the tex-solution ;) Looking forward for your ideas. Thanks in advance – moospit Aug 7 '13 at 14:13
  • Hmm, that comment was too fast. Is it possible to remove the ; and use either a new line or `\\` – percusse Aug 7 '13 at 14:52
2

I continue to believe that every table built with \multirow can be improved by removing it. This is one of the cases:

\documentclass{article}
\usepackage{xstring}

\begin{document}
% test input
\newcommand{\myinput}{IP1:FQDN1;IP2:FQDN2}

\newcommand{\splitstring}[1]{%
  \saveexpandmode\expandarg
  % split input into table rows
  \StrSubstitute{#1}{;}{\noexpand\\}[\tmp]%
  % split columns
  \StrSubstitute{\tmp}{:}{&}[\tmp]%
  \restoreexpandmode
  \begin{tabular}{@{}ll@{}}\tmp\end{tabular}
}

\begin{tabular}{l|l}
Hosts & \splitstring{\myinput}
\end{tabular}

\bigskip

\renewcommand{\myinput}{}

\begin{tabular}{l|l}
Hosts & \splitstring{\myinput}
\end{tabular}

\end{document}

The idea is to build an inner table with the addresses. I've also shown that it works also with empty input.

enter image description here

  • Thank you very much, you saved my day. This is the perfect solution. I was thinking too much in the \multirow-way. Mostly the simplest solution is the best. – moospit Aug 7 '13 at 17:37

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.