2

I am working on a macro which will build revision history table in my document using Git tags. Each published version of my document has a tag with short description. I want to use them to automatically build revision table while compiling with XeLaTeX in Windows.

Using Git command below i get all information I need to create the revision history table. Output is formatted in CSV-like format.

git for-each-ref --format='%(refname:short); %(taggerdate:short); %(subject); %(*authorname); %(*authoremail)' refs/tags

I modified a macro for parsing CSV file so that I can read the output of the Git command and build my revision history table (not formatted yet as table in the script).

\documentclass{article}

\newcommand{\printrow}[5]{tag: #1 (#3) at #2 by #4 #5 + \\}

\begin{document}

\def\readrow #1;#2;#3;#4;#5;{\ifx^#5^\else 
   \printrow{#1}{#2}{#3}{#4}{#5}%
   \expandafter\readrow\fi}
\def\startread {\readrow} % skip first row
\begingroup
   \endlinechar=`; \everyeof={;;;;;} \catcode`\@=11
   \expandafter \startread \@@input |"git revhist" %
\endgroup % 

\end{document}

It works with pre-saved output of the command, but when I paste the full command string into \@@input and try to compile (xelatex -shell-escape .\read-git-history-test.tex), XeLaTeX complains:

{|"git for-each-ref --format='
! Paragraph ended before \@iinput was complete.
<to be read again>
                   \par

I did a workaround by defining Git alias (git revhist -see below) but if one of my colleagues will try to compile it without defining the alias, it will fail. No, they do not read REDAME's...

git config --global alias.revhist "for-each-ref --format='%(refname:short); %(taggerdate:short); %(subject); %(*authorname); %(*authoremail);' refs/tags"

Is it possible to call the full Git command in *.tex file not to corrupt it by the % signs?

8
  • By the way you may want to learn how catcode works in TeX properly by yourself first. Read TeX by topic or TeXbook
    – user202729
    Apr 15, 2022 at 12:03
  • 2
    use \@percentchar or locally make % have catcode 12 Apr 15, 2022 at 12:09
  • They used to have write18, nowadays it's just input-pipe. pdftex - \write18 pass % through to shell - TeX - LaTeX Stack Exchange Mostly-same principle though. (both \input and \write x-expand the argument)
    – user202729
    Apr 15, 2022 at 17:17
  • but I don't know what the \input|... is called (to search for it).
    – user202729
    Apr 15, 2022 at 17:23
  • 1
    You can try to use iexec package, which does it all for you: escaping chars, checking exit code of the script, etc.
    – yegor256
    Oct 20, 2022 at 4:37

1 Answer 1

1

I used \@percentchar as suggested by @DavidCarlisle, and it works as expected.

\def\readrow #1;#2;#3;#4;#5;{\ifx^#5^\else
   \addrevision{#1}{#2}{#4}{#3}%
   \expandafter\readrow\fi}
\def\startread {\readrow} % skip first row
\begingroup
   \endlinechar=`; \everyeof={;;;;;} \catcode`\@=11
   \expandafter \startread \@@input |"git for-each-ref --format='\@percentchar(refname:short); \@percentchar(taggerdate:short); \@percentchar(subject); \@percentchar(*authorname); \@percentchar(*authoremail)' refs/tags" %
\endgroup %

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .