3

Question

How is possible to automatically create a glossary with name, definition and each page in which the current word is present in a set of tex files (more than one), without explicit references such as \gls{myword}...?

Situation

  • I have one (two) dictionary with the entry and the definition, that I'm able to export in any convenient format.

  • I have a bunch of LaTeX files (around 50 between beamer presentations and articles) in which some of those words are present, without any referring system as \gls{myword}...
    They logically belong to different group types, for simplicity let's say T, as Text, and B as beamer presentation.
    They all have a common macro header that I can modify to include package or macros in all the files at the same time.

  • I have no resources to manually edit all the files adding reference as \gls{myword}, and it is too risky to blindly do it with sed.

I need

  • I need a glossary with name, definition and document name (or symbol T1: p.2,4-7,12; T3:p.4,8; B12:p1) + page where it is possible to find it.

What I've done

  • I made a bash script to strip the tex source files comments, used grep to find in which file each word is present. With some more gym I can keep count of the \section or \begin{frame} current numbers and start to build the index.
    It remains open the problem in beamer to correct count the current slide when allowframebreaks breaks a slide...

  • I checked some introduction book on glossaries

  • Of course I search for a solution in not less than 20-25 Q&A on this site before posting this question (but they all rely on the \gls{myword} mechanism).

Conclusion

I've the feeling I'm trying to reinvent the wheel.
Is there a LaTeX way/tool to do it without citing from inside the text?
(It's even simple :-)?)

2
  • If you're not going to use the indexing interface provided by glossaries (\gls) then there's no point using that package for indexing. It seems more as though you want a concordance like How to generate an automatic index (concordance) in a large file?. Commented Apr 16, 2018 at 11:00
  • 2
    @NicolaTalbot Thanks for the link. Among the Q&A seen I missed that one, but I bumped in some of the manuals you wrote. I quick read a couple and I find them really well written. IMHO yours a gift enhanced with care and application. You did (are doing) a great work. Thanks. BTW With the time constrains I had I found a quick (not so much) and dirty (a lot) solution via the pdf files and bashisms.
    – Hastur
    Commented Apr 16, 2018 at 11:34

1 Answer 1

2

A quick (not so much) and dirty (a lot) solution

Thinking a little out of the LaTeX box, I approached the problem starting from the PDF files. Far to be a LaTeX solution, or the right one, the following is a quick and really dirty solution, working in the specific case, but it could be a base to a more complete solution.

With an external tool (pdfgrep [1,2]), I was able to search for a keyword in a PDF files group (in a similar way grep does inside a text files group).

Then it was question to parse the output and format the output in the preferred way. Here below the script (it's a working file not even cleaned; it should be rewritten in pure awk and in a plan way).
It's ugly, with hump(s) but it works (enough). It can be called from (or included in) the script that parses the dictionary file (the one with keywords and definition) to produce an output that we can include in the tex file.

You may run the script with the keyword as parameter and it answers with a string like [File1:1-3,5,9-12; File3:2,7-122].

#!/bin/bash
# ----------------------------------------------- 
#   Search for a key in all pdf files
#    e.g L1.pdf L2.pdf...
#     and answers with collapsed page number
#      for each file [L1: 1, 3-7; L2:2-12, 22]
# ----------------------------------------------- 
#    Version 2.0 
#      Mon Apr 16 13:50:42 2018
# ----------------------------------------------- 

Key="$1"
Where='*pdf'

All=$(
pdfgrep -Hin "$Key" $Where | \
     awk -F ':' '{ if ($2 != P[$1,CountA[$1]])
                 {CountA[$1]++;P[$1,CountA[$1]]=$2} }
                 END{for (i in CountA ) {
                   printf ("%s ", i); 
                   for (j = 1 ; j<=CountA[i]; j++) {
                       printf( "%d ", j)
                       } ; 
                       printf "\n"  
                       }
                   }' | sort 
)
## Collapsing number sequences 1 2 3 4 7 9 10--> 1-4, 7. 9-10
A3=$(echo "$All" | awk '{ printf ("%s: ",$1) ; r=nxt=0; for(i=2;i<=NF;i++) if($i+1==$(i+1)){ if(!r) r=$i"-";nxt=$(i+1) } else { printf "%s%s",(r)?r nxt:$i,(i==NF)?ORS:FS; r=0 } } END{printf("\n") }')

## Collapsing output in 1 line [L1: 1, 3-7; L2:2-12, 22]
echo "$A3" | awk 'BEGIN{ printf "[";MySep=""}{printf ("%s%s", MySep,$0);MySep="; "} END{ printf "]\n"}' | sed 's/\[\: \]//g ; s/\.pdf//g'

exit 0

Ps> Really the script needs a brilliant surgeon help for its humps
- Hump which hump? [3]

You must log in to answer this question.

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