3

Given list of file names, I wanted to obtain the word count of each file, but by using either \forcsvlist or \docsvlist and pass the file name to bash from Latex without hardcoding the file name in the bash command, but by using the argument passed to the \docsvlist.

I can't get it to work. Here is a MWE of what I am trying to do, which does not work at all

\documentclass{article}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{bashful}
\begin{document}
\renewcommand*{\do}[1]{%
The file #1 has size
\bash
wc #1
\END
\bashStdout

}
\docsvlist{foo.log,foo.tex}
\end{document}

And now

 pdflatex --shell-esc  foo.tex

(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
! Use of \bashIII doesn't match its definition.
<recently read> w

l.17 \docsvlist{foo.log,foo.tex}

I think I need to expandafter or expandonce or one of these commands Which I really still do not understand how these work or where to use them. The package document does not have many examples to help understand how to use these commands.

If I move the bash command outside, then it works of course:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{bashful}
\begin{document}
\renewcommand*{\do}[1]{%
The file #1 has size

}
\docsvlist{foo.log,foo.tex}

\bash
wc foo.tex
\END
\bashStdout    
\end{document}

But I need to use the bash commands on the list items, pass the current list item #1 to the bash command, and get the output of the bash command to use.

Is it possible to do the above?

TL 2015

  • 1
    It's always the same problem: \bash uses verbatim, so it can't be in the argument to another command. – egreg Jul 29 '15 at 6:53
4

You can't use \bash in the argument to another command, because it uses some variant of verbatim mode.

There's a simple way to solve this without extra packages, using “piped input”: if the first token in the argument to the primitive \input command is |, then the file name is taken as a shell command to execute with, essentially, the TeX file as standard output. Quotes around the file name are allowed (and recommended, in this case) both by TeX Live and MiKTeX.

\documentclass{article}
\usepackage{etoolbox}

\makeatletter
\newcommand{\shellcommand}[1]{\@@input"|#1"}
\makeatother

\begin{document}

\renewcommand*{\do}[1]{%
  The file #1 has size \texttt{\shellcommand{wc #1}}\par
}

\docsvlist{fontaw.tex,fontaw.log}
\end{document}

enter image description here

  • thanks, did not know about the \@@input, looks very useful. – Nasser Jul 29 '15 at 8:03
4

I found a work around !

It looks like the command splice does not treat its argument as verbatim. Hence the following works!

\documentclass{article}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{bashful}
\begin{document}

\renewcommand*{\do}[1]{%
The file #1 has size \splice{wc #1}

}
\docsvlist{foo.log,foo.tex}
\end{document}

Compiled using pdflatex --shell-esc foo.tex and the PDF contains the information I wanted:

Mathematica graphics

I found about splice reading more the bashful document

Mathematica graphics

The above is very useful.

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.