improve tex esport

This commit is contained in:
nnduc
2026-11-21 11:25:27 +07:00
parent 6c1b3d13f2
commit 7ec538537c
+222 -121
View File
@@ -1,47 +1,101 @@
#!/usr/bin/env bash
# export_tex.sh — collect all files needed to compile a .tex file
# Usage: ./export_tex.sh [manuscript.tex] [output_dir]
# Defaults: first .tex in current dir; output_dir → ./export
# export_tex.sh — export a LaTeX project as a zip, the way Overleaf's
# "Download Source" does: project-relative paths at the zip root (no wrapping
# folder), named <ProjectName>.zip with every non-letter/digit replaced by "_".
#
# Usage: ./export_tex.sh [options] [main.tex] [output]
# main.tex defaults to the first .tex in the current dir
# output *.zip → write that zip; anything else → plain directory
# (default: ./<ProjectName>.zip)
# Options:
# -a, --all Overleaf-style: take every file in the project tree except
# build artifacts, instead of tracing what main.tex uses
# -n, --name N project name used for the zip (default: source dir name)
# -h, --help show this help
set -euo pipefail
usage() { sed -n '2,/^$/{s/^# \{0,1\}//p}' "$0"; exit "${1:-0}"; }
# ── arguments ────────────────────────────────────────────────────────────────
TEX_FILE="${1:-}"
MODE=trace
PROJECT_NAME=""
POS=()
while (($#)); do
case "$1" in
-a|--all) MODE=all ;;
-n|--name) PROJECT_NAME="${2:?--name needs a value}"; shift ;;
-h|--help) usage ;;
--) shift; POS+=("$@"); break ;;
-*) echo "Unknown option: $1" >&2; usage 1 ;;
*) POS+=("$1") ;;
esac
shift
done
TEX_FILE="${POS[0]:-}"
if [[ -z "$TEX_FILE" ]]; then
TEX_FILE=$(ls *.tex 2>/dev/null | head -1)
[[ -z "$TEX_FILE" ]] && { echo "No .tex file found."; exit 1; }
TEX_FILE=$(find . -maxdepth 1 -name '*.tex' -print -quit)
[[ -n "$TEX_FILE" ]] || { echo "No .tex file found." >&2; exit 1; }
fi
[[ -f "$TEX_FILE" ]] || { echo "File not found: $TEX_FILE"; exit 1; }
[[ -f "$TEX_FILE" ]] || { echo "File not found: $TEX_FILE" >&2; exit 1; }
EXPORT_DIR="${2:-export}"
SRC_DIR="$(dirname "$(realpath "$TEX_FILE")")"
TEX_FILE="$(realpath "$TEX_FILE")"
SRC_DIR="$(dirname "$TEX_FILE")"
TEX_BASE="$(basename "$TEX_FILE")"
JOB="${TEX_BASE%.tex}"
mkdir -p "$EXPORT_DIR"
# Overleaf: project.name.replace(/[^\p{L}\p{Nd}]/gu, '_')
PROJECT_NAME="${PROJECT_NAME:-$(basename "$SRC_DIR")}"
SAFE_NAME="$(perl -CSA -e 'my $s = shift; $s =~ s/[^\p{L}\p{Nd}]/_/g; print $s' "$PROJECT_NAME")"
IMAGE_EXTS=(".pdf" ".png" ".jpg" ".jpeg" ".eps" ".svg" ".PNG" ".JPG" ".JPEG")
OUTPUT="${POS[1]:-$SAFE_NAME.zip}"
if [[ "$OUTPUT" == *.zip ]]; then
ZIP_OUT="$(realpath -m "$OUTPUT")"
EXPORT_DIR="$(mktemp -d)"
trap 'rm -rf "$EXPORT_DIR"' EXIT
else
ZIP_OUT=""
EXPORT_DIR="$(realpath -m "$OUTPUT")"
mkdir -p "$EXPORT_DIR"
fi
# Track already-processed tex files to avoid infinite loops
declare -A VISITED_TEX
# latexmk output/aux dirs (from latexmkrc), used for .bbl lookup and exclusion
BUILD_DIRS=()
while IFS= read -r d; do
[[ -n "$d" ]] && BUILD_DIRS+=("${d%/}")
done < <(grep -hoP '\$(out|aux)_dir\s*=\s*["'\'']\K[^"'\'']+' \
"$SRC_DIR/latexmkrc" "$SRC_DIR/.latexmkrc" 2>/dev/null || true)
# find(1) arguments that skip VCS/hidden dirs, build dirs and our own output,
# so earlier exports are never picked up as sources
PRUNE=( \( -type d -path "$SRC_DIR/.*" -o -path "$EXPORT_DIR" )
[[ -n "$ZIP_OUT" ]] && PRUNE+=( -o -path "$ZIP_OUT" )
for d in "${BUILD_DIRS[@]}"; do PRUNE+=( -o -path "$SRC_DIR/$d" ); done
PRUNE+=( \) -prune -o )
# ── helpers ──────────────────────────────────────────────────────────────────
copy_rel() {
# Copy a file to EXPORT_DIR preserving its path relative to SRC_DIR.
local src="$1"
[[ -f "$src" ]] || { echo " [missing] $src"; return; }
local rel="${src#$SRC_DIR/}"
[[ -f "$src" ]] || { echo " [missing] $src"; return 0; }
if [[ "$src" != "$SRC_DIR"/* ]]; then
echo " [outside] $src (not under $SRC_DIR, skipped)"; return 0
fi
local rel="${src#"$SRC_DIR"/}"
local dest="$EXPORT_DIR/$rel"
[[ -e "$dest" ]] && return 0
mkdir -p "$(dirname "$dest")"
cp -v "$src" "$dest"
cp "$src" "$dest"
echo " $rel"
}
find_recursive() {
# Find file by basename+ext under SRC_DIR; prefer shallower paths.
local base="$1"; shift
local found=""
local found="" ext
for ext in "$@"; do
# Sort by path depth (fewer slashes = shallower) then alphabetically
found=$(find "$SRC_DIR" -name "${base}${ext}" \
found=$(find "$SRC_DIR" "${PRUNE[@]}" -type f -name "${base}${ext}" -print \
| awk '{ print gsub(/\//,"/")" "$0 }' | sort -n | head -1 \
| cut -d' ' -f2-)
[[ -n "$found" ]] && break
@@ -49,125 +103,172 @@ find_recursive() {
echo "$found"
}
copy_image() {
local img="$1"
# Normalise: strip leading ./ if present
img="${img#./}"
# 1. Try path as written in .tex (relative to SRC_DIR), with/without ext
if [[ "$img" == *.* ]]; then
local f="$SRC_DIR/$img"
if [[ -f "$f" ]]; then copy_rel "$f"; return; fi
else
for ext in "${IMAGE_EXTS[@]}"; do
local f="$SRC_DIR/${img}${ext}"
if [[ -f "$f" ]]; then copy_rel "$f"; return; fi
done
fi
# 2. Fallback: recursive search by basename only
local base="${img##*/}"
local f
f=$(find_recursive "$base" "${IMAGE_EXTS[@]}")
if [[ -n "$f" ]]; then
copy_rel "$f"
else
echo " [missing] $img"
fi
strip_comments() {
# Drop everything after an unescaped % on each line
sed -E 's/(^|[^\\])%.*/\1/' "$1" | tr -d '\r'
}
args_of() {
# Print the mandatory {arg} of each \cmd (optional [..] allowed), one per
# line, splitting comma lists. $1 = regex alternation of command names.
grep -oP "\\\\($1)\\*?(\\[[^\\]]*\\])?\\{[^}]+\\}" \
| grep -oP '\{\K[^}]+' | tr ',' '\n' | sed -E 's/^\s+|\s+$//g' \
| grep -v '^$' || true
}
# ── mode "all": whole project tree minus build artifacts ─────────────────────
BUILD_EXT='aux|log|out|toc|lof|lot|loa|fls|fdb_latexmk|synctex\.gz|synctex|bcf|run\.xml|blg|bbl|nav|snm|vrb|idx|ilg|ind|glo|gls|glg|ist|xdv|dvi|thm|spl|brf'
if [[ "$MODE" == all ]]; then
echo "Collecting project tree from $SRC_DIR"
while IFS= read -r -d '' f; do
name="${f##*/}"
[[ "$name" =~ \.($BUILD_EXT)$ ]] && continue
[[ "$name" =~ ~$ || "$name" == *.swp ]] && continue
# earlier exports lying around in the project root
[[ "$f" == "$SRC_DIR/$name" && "$name" == *.zip ]] && continue
# compiled PDF of any .tex in the tree
[[ "$name" == *.pdf && -f "${f%.pdf}.tex" ]] && continue
copy_rel "$f"
done < <(find "$SRC_DIR" "${PRUNE[@]}" -type f -print0)
fi
# ── mode "trace": follow what main.tex actually uses ─────────────────────────
IMAGE_EXTS=(".pdf" ".png" ".jpg" ".jpeg" ".eps" ".svg" ".PNG" ".JPG" ".JPEG" ".PDF")
GRAPHICS_DIRS=("") # \graphicspath dirs, tried after the path as written
declare -A VISITED_TEX
copy_image() {
local img="${1#./}"
[[ "$img" == *"#"* ]] && return 0 # macro parameter such as #1
local dir ext
for dir in "${GRAPHICS_DIRS[@]}"; do
if [[ "$img" == *.* && -f "$SRC_DIR/$dir$img" ]]; then
copy_rel "$SRC_DIR/$dir$img"; return 0
fi
for ext in "${IMAGE_EXTS[@]}"; do
if [[ -f "$SRC_DIR/$dir$img$ext" ]]; then
copy_rel "$SRC_DIR/$dir$img$ext"; return 0
fi
done
done
# Fallback: recursive search by basename only
local f
f=$(find_recursive "${img##*/}" "" "${IMAGE_EXTS[@]}")
if [[ -n "$f" ]]; then copy_rel "$f"; else echo " [missing] $img"; fi
}
# ── recursive tex processor ───────────────────────────────────────────────────
process_tex() {
local tex_path
tex_path="$(realpath "$1")"
[[ -n "${VISITED_TEX[$tex_path]:-}" ]] && return
[[ -n "${VISITED_TEX[$tex_path]:-}" ]] && return 0
VISITED_TEX["$tex_path"]=1
[[ -f "$tex_path" ]] || { echo " [missing tex] $tex_path"; return; }
copy_rel "$tex_path"
# \input{file} or \include{file}
while IFS= read -r inc; do
# Try with and without .tex extension
local candidate=""
for try in "$SRC_DIR/${inc}.tex" "$SRC_DIR/${inc}"; do
[[ -f "$try" ]] && { candidate="$try"; break; }
done
if [[ -n "$candidate" ]]; then
process_tex "$candidate"
else
echo " [missing tex] $inc"
fi
done < <(grep -oP '\\(input|include)\{[^}]+\}' "$tex_path" \
| grep -oP '(?<=\{)[^}]+' | tr -d '\r')
local body
body="$(strip_comments "$tex_path")"
# \includegraphics
# \input, \include, \subfile, \InputIfFileExists — relative to the main
# file's directory, as LaTeX (and Overleaf) resolve them
local inc try
while IFS= read -r inc; do
for try in "$SRC_DIR/$inc.tex" "$SRC_DIR/$inc"; do
[[ -f "$try" ]] && { process_tex "$try"; continue 2; }
done
echo " [missing tex] $inc"
done < <(args_of 'input|include|subfile|InputIfFileExists' <<<"$body")
# \import{dir}{file} / \subimport{dir}{file}
while IFS=$'\t' read -r dir file; do
for try in "$SRC_DIR/$dir$file.tex" "$SRC_DIR/$dir$file"; do
[[ -f "$try" ]] && { process_tex "$try"; continue 2; }
done
echo " [missing tex] $dir$file"
done < <(grep -oP '\\(sub)?import\*?\{[^}]*\}\{[^}]+\}' <<<"$body" \
| sed -E 's/.*\{([^}]*)\}\{([^}]+)\}/\1\t\2/' || true)
# \graphicspath{{img/}{fig/}}
local d
while IFS= read -r d; do
[[ -n "$d" ]] && GRAPHICS_DIRS+=("${d%/}/")
done < <(grep -oP '\\graphicspath\{.*\}' <<<"$body" \
| grep -oP '(?<=\{)[^{}]+(?=\})' || true)
# One-argument macros that wrap \includegraphics{#1}
local macros="includegraphics|includesvg|includepdf" m
while IFS= read -r m; do
[[ -n "$m" ]] && macros+="|$m"
done < <(grep -oP '\\(re)?newcommand\{?\\\w+\}?\[1\]\{.*\\includegraphics(\[[^\]]*\])?\{#1\}' <<<"$body" \
| grep -oP '(?<=command)\{?\\\K\w+' || true)
local img
while IFS= read -r img; do
copy_image "$img"
done < <(grep -oP '\\includegraphics(\[[^\]]*\])?\{[^}]+\}' "$tex_path" \
| grep -oP '(?<=\{)[^}]+' | tr -d '\r')
done < <(grep -oP "\\\\($macros)\\*?(\\[[^\\]]*\\])?\\{[^}]+\\}" <<<"$body" \
| grep -oP '\{\K[^}]+' | sort -u || true)
}
# ── 1. recurse through all .tex files ────────────────────────────────────────
process_tex "$TEX_FILE"
if [[ "$MODE" == trace ]]; then
echo "Tracing $TEX_BASE"
process_tex "$TEX_FILE"
# ── 2. document class (.cls) ──────────────────────────────────────────────────
CLS=$(grep -oP '\\documentclass\[?[^\]]*\]?\{[^}]+\}' "$TEX_FILE" \
| grep -oP '(?<=\{)[^}]+' | tr -d '\r' | head -1)
if [[ -n "$CLS" ]]; then
f=$(find_recursive "$CLS" ".cls")
if [[ -n "$f" ]]; then
# Local packages/classes/styles: an Overleaf project carries these files,
# and classes load helpers (.clo/.cfg/.def) that can't be traced reliably
while IFS= read -r -d '' f; do
copy_rel "$f"
else
echo " [system] ${CLS}.cls (not local, assumed installed)"
fi
done < <(find "$SRC_DIR" -maxdepth 1 -type f \( -name '*.cls' -o -name '*.sty' \
-o -name '*.clo' -o -name '*.cfg' -o -name '*.def' -o -name '*.ldf' \
-o -name '*.bst' -o -name '*.bbx' -o -name '*.cbx' \) -print0)
SEARCH_FILES=("${!VISITED_TEX[@]}")
ALL_BODY="$(for f in "${SEARCH_FILES[@]}"; do strip_comments "$f"; done)"
# Document class / packages kept in subdirectories
while IFS= read -r p; do
f=$(find_recursive "$p" ".cls" ".sty")
[[ -n "$f" ]] && copy_rel "$f"
done < <(args_of 'documentclass|usepackage|RequirePackage|LoadClass' <<<"$ALL_BODY" | sort -u)
# Bibliography style (.bst) and biblatex style= / bibstyle= / citestyle=
while IFS= read -r s; do
f=$(find_recursive "$s" ".bst" ".bbx" ".cbx")
[[ -n "$f" ]] && copy_rel "$f"
done < <({ args_of 'bibliographystyle' <<<"$ALL_BODY"
grep -oP '\\usepackage\[[^\]]*\]\{biblatex\}' <<<"$ALL_BODY" \
| grep -oP '(bib|cite)?style\s*=\s*\{?\K[^},\]\s]+' || true; } | sort -u)
# Bibliography databases: \bibliography{a,b} and \addbibresource{x.bib}
while IFS= read -r bib; do
bib="${bib%.bib}"
if [[ -f "$SRC_DIR/$bib.bib" ]]; then
copy_rel "$SRC_DIR/$bib.bib"
else
f=$(find_recursive "${bib##*/}" ".bib")
if [[ -n "$f" ]]; then copy_rel "$f"; else echo " [missing] $bib.bib"; fi
fi
done < <(args_of 'bibliography|addbibresource|addglobalbib' <<<"$ALL_BODY" | sort -u)
fi
# Collect all files to search for bibliography settings: .tex + any local .cls/.sty
mapfile -t SEARCH_FILES < <(
printf '%s\n' "${!VISITED_TEX[@]}"
find "$SRC_DIR" -maxdepth 1 \( -name "*.cls" -o -name "*.sty" \) 2>/dev/null
)
# ── 3. bibliography style (.bst / .bbx) ──────────────────────────────────────
BST=$(grep -rhP '\\bibliographystyle\{[^}]+\}' "${SEARCH_FILES[@]}" 2>/dev/null \
| grep -oP '(?<=\{)[^}]+' | tr -d '\r' | head -1 || true)
if [[ -n "$BST" ]]; then
f=$(find_recursive "$BST" ".bst" ".bbx")
if [[ -n "$f" ]]; then
copy_rel "$f"
else
echo " [system] ${BST}.bst (not local, assumed installed)"
# ── pre-built .bbl ───────────────────────────────────────────────────────────
# Not part of an Overleaf source zip, but journals/arXiv want it. Look next to
# the .tex, then in latexmk's out/aux dirs; always export it beside the .tex.
for d in "" "${BUILD_DIRS[@]}"; do
BBL="$SRC_DIR/${d:+$d/}$JOB.bbl"
if [[ -f "$BBL" ]]; then
cp "$BBL" "$EXPORT_DIR/$JOB.bbl"
echo " $JOB.bbl (pre-built)"
break
fi
fi
# also copy any local .bbx / .cbx referenced by biblatex style option
# matches both style= and bibstyle=
grep -rhP '\\usepackage.*biblatex' "${SEARCH_FILES[@]}" 2>/dev/null \
| grep -oP '(?:bib)?style\s*=\s*\{?[^},\]]+' | grep -oP '[^={} \r]+$' \
| tr -d '\r' | sort -u \
| while IFS= read -r sty; do
for ext in ".bbx" ".cbx"; do
f=$(find_recursive "$sty" "$ext")
[[ -n "$f" ]] && copy_rel "$f"
done
done || true
# ── 4. bibliography database (.bib) ──────────────────────────────────────────
# handles both \bibliography{} (BibTeX) and \addbibresource{} (biblatex)
while IFS= read -r bib; do
bib="${bib%.bib}" # strip .bib suffix if already present
f=$(find_recursive "$bib" ".bib")
[[ -n "$f" ]] && copy_rel "$f" || echo " [missing] ${bib}.bib"
done < <({ grep -rhP '\\bibliography\{[^}]+\}' "${SEARCH_FILES[@]}" 2>/dev/null \
| grep -oP '(?<=\{)[^}]+' | tr ',' '\n' | tr -d ' \r'
grep -rhP '\\addbibresource\{[^}]+\}' "${SEARCH_FILES[@]}" 2>/dev/null \
| grep -oP '(?<=\{)[^}]+' | tr -d '\r'; } \
| sort -u || true)
# ── 5. .bbl (pre-built bibliography, if present) ─────────────────────────────
BBL="$SRC_DIR/${TEX_BASE%.tex}.bbl"
[[ -f "$BBL" ]] && copy_rel "$BBL"
done
# ── package ──────────────────────────────────────────────────────────────────
echo ""
echo "Done. Files exported to: $EXPORT_DIR/"
if [[ -n "$ZIP_OUT" ]]; then
rm -f "$ZIP_OUT"
mkdir -p "$(dirname "$ZIP_OUT")"
(cd "$EXPORT_DIR" && zip -qrXD "$ZIP_OUT" .)
echo "Done. $(zipinfo -1 "$ZIP_OUT" | wc -l) files → $ZIP_OUT"
else
echo "Done. Files exported to: $EXPORT_DIR/"
fi