Guest User

tex-which, phaoMath blog, https://phaomath.blogspot.com/

a guest
Apr 8th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function tex_search (
  4.   TEX_PKGS_DIR=/usr/share/texlive/texmf-dist/tex/
  5.  
  6.   echo -n Looking for \\$1 in $TEX_PKGS_DIR ...
  7.  
  8.   # This amount of needed \\ is because we're doing escapes for bash strings and also for grep.
  9.   results_fname=$(mktemp)
  10.   find $TEX_PKGS_DIR -type f \( -iname '*.tex' -or -iname '*.sty' -or -iname '*.cls' \) -exec grep -Ei "(command|cmd|new|def|decl)[^\\\\]*\\\\$1[^a-zA-Z]" '{}' + | grep "\\\\$1[^a-zA-Z]" > $results_fname
  11.  
  12.   echo ' (done)'
  13.   echo
  14.   echo Found possible symbol definition for \\$1 in files:
  15.  
  16.   # read -r makes it treat backslash not as an escape character, but as part of the string.
  17.   cat $results_fname | while read -r occur
  18.   do
  19.     basename $(echo $occur | cut -d ':' -f 1)
  20.   done | sort -u | tr '\n' '\t' | fmt
  21.  
  22.   echo
  23.   echo Full results:
  24.  
  25.   cat $results_fname | while read -r occur
  26.   do
  27.     full_path=$(echo $occur | cut -d ':' -f 1)
  28.     code=$(echo $occur | cut -d ':' -f 2)
  29.     full_dir=$(dirname $full_path)
  30.     dir=${full_dir#*$TEX_PKGS_DIR}
  31.     echo $(basename $full_path) in $dir
  32.     echo -en "\t"
  33.     echo $code | tr -d '\n' | grep -i "\\\\$1" --color=always
  34.   done
  35.  
  36.   rm $results_fname
  37. )
  38.  
  39. # The option -R in less is so that it handle colors well.
  40. tex_search "$1" | less -R
  41.  
Advertisement
Add Comment
Please, Sign In to add comment