#!/bin/bash
# -*- coding: utf-8 -*-
# File: process_figures_modules/04_compilation.src
# Figure compilation and final assembly functions
# Compile figure tex files into FINAL.tex
compile_figure_tex_files() {
# Determine path style based on engine
# Tectonic requires absolute paths for image loading
local figure_path_base="$SCITEX_WRITER_FIGURE_JPG_DIR"
if [ "${SCITEX_WRITER_ENGINE}" = "tectonic" ] || [ "${SCITEX_WRITER_SELECTED_ENGINE}" = "tectonic" ]; then
# Convert to absolute path for tectonic
figure_path_base="$(cd "$(dirname "$SCITEX_WRITER_FIGURE_JPG_DIR")" 2>/dev/null && pwd)/$(basename "$SCITEX_WRITER_FIGURE_JPG_DIR")"
echo_info " Using absolute paths for tectonic engine"
fi
echo "% Generated by compile_figure_tex_files()" >"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo "% This file includes all figure files in order" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo "" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
# First, check if there are any real figure files (not just the header)
local figure_files=($(find "$SCITEX_WRITER_FIGURE_COMPILED_DIR" -maxdepth 1 \( -name "[0-9]*.tex" \) ! -name "00_Figures_Header.tex" | sort))
local has_real_figures=false
if [ ${#figure_files[@]} -gt 0 ]; then
has_real_figures=true
# Don't add section header here - it's already in base.tex
fi
# Handle figure tex files
for fig_tex in $(find "$SCITEX_WRITER_FIGURE_COMPILED_DIR" -maxdepth 1 \( -name "[0-9]*.tex" \) | sort); do
[ -e "$fig_tex" ] || continue
local basename=$(basename "$fig_tex")
# Skip the header file completely if we have real figures
if [[ "$basename" == "00_Figures_Header.tex" ]]; then
if [ "$has_real_figures" = true ]; then
continue
else
# Only use the header template if no real figures exist
cat "$fig_tex" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
continue
fi
fi
local figure_id=""
local figure_number=""
local figure_title=""
local image_path=""
local width="0.9\\textwidth"
local figure_type="image"
local caption_content=""
# Extract figure ID from filename
if [[ "$basename" =~ ^([0-9]+.*).tex$ ]]; then
figure_id="${BASH_REMATCH[1]}"
if [[ "$figure_id" =~ ^([0-9]+)_ ]]; then
figure_number="${BASH_REMATCH[1]}"
else
figure_number="$figure_id"
fi
fi
# Read caption content from the original caption file
local original_caption_file="$SCITEX_WRITER_FIGURE_CAPTION_MEDIA_DIR/${figure_id}.tex"
if [ -f "$original_caption_file" ] || [ -L "$original_caption_file" ]; then
caption_content=$(cat "$original_caption_file" | grep -v "^%" | grep -v "^$")
# Extract title from first textbf for the figure comment
if [[ "$caption_content" =~ \\textbf\{([^}]*)\} ]]; then
figure_title="${BASH_REMATCH[1]}"
fi
fi
# Use simple fallback if no caption found
if [ -z "$caption_content" ]; then
caption_content="\\caption{\\textbf{Figure $figure_number}\\\\Description for figure $figure_number.}"
fi
# Determine image path (use figure_path_base for tectonic compatibility)
image_path="$figure_path_base/${figure_id}.jpg"
if [ ! -f "$image_path" ]; then
# Try without ID
image_path="$figure_path_base/${figure_number}.jpg"
fi
# Write figure environment
echo "% Figure $figure_number" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
# Use [htbp] for all figures to allow flexible placement
echo "\\begin{figure*}[htbp]" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
local _fig_bookmark="Figure $figure_number"
[ -n "$figure_title" ] && _fig_bookmark="Figure $figure_number --- ${figure_title%.}"
echo " \\pdfbookmark[2]{$_fig_bookmark}{.$figure_number}" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo " \\centering" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo " \\includegraphics[width=$width]{$image_path}" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo " $caption_content" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
# Only add label if caption doesn't already contain one
if ! echo "$caption_content" | grep -q '\\label{fig:'; then
echo " \\label{fig:${figure_id}}" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
fi
echo "\\end{figure*}" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
echo "" >>"$SCITEX_WRITER_FIGURE_COMPILED_FILE"
done
}
# Toggle figure visibility
handle_figure_visibility() {
local no_figs="$1"
if [[ "$no_figs" == true ]]; then
_toggle_figures disable
else
# Optional: Run Python optimizer on generated JPGs
optimize_figures_with_python "$no_figs"
[[ -n $(find "$SCITEX_WRITER_FIGURE_JPG_DIR" -name "*.jpg") ]] && _toggle_figures enable || _toggle_figures disable
fi
}
# Internal function to toggle figures
_toggle_figures() {
local state="$1"
if [ "$state" == "enable" ]; then
# Enable figures in LaTeX
echo "% Figures enabled" >"$SCITEX_WRITER_FIGURE_COMPILED_DIR/.figures_enabled"
else
# Disable figures in LaTeX
rm -f "$SCITEX_WRITER_FIGURE_COMPILED_DIR/.figures_enabled"
fi
}
# Optimize figures with Python
optimize_figures_with_python() {
local no_figs="$1"
[ "$no_figs" = true ] && return 0
echo_info " Optimizing figures with Python script..."
local python_script="$THIS_DIR/../python/optimize_figures.py"
if [ -f "$python_script" ] && command -v python3 >/dev/null 2>&1; then
python3 "$python_script" \
--input-dir "$SCITEX_WRITER_FIGURE_JPG_DIR" \
--max-width 2400 \
--max-height 1800 \
--quality 95
fi
}
# Create placeholder for missing figures
create_placeholder_jpg() {
local output_file="$1"
local figure_name="$2"
# Use ImageMagick to create a placeholder
if command -v convert >/dev/null 2>&1; then
convert -size 800x600 xc:lightgray \
-gravity center \
-pointsize 48 \
-annotate +0+0 "Missing Figure\n$figure_name" \
"$output_file"
else
# Create a minimal placeholder
echo "Missing figure: $figure_name" >"${output_file%.jpg}.txt"
fi
}
# Check and create placeholders for missing figures
check_and_create_placeholders() {
echo_info "Checking for missing figures..."
for caption_file in "$SCITEX_WRITER_FIGURE_CAPTION_MEDIA_DIR"/[0-9]*_*.tex; do
[ -e "$caption_file" ] || continue
local base_name=$(basename "$caption_file" .tex)
# Skip panel captions
if [[ "$base_name" =~ ^[0-9]+[a-z]_ ]]; then
continue
fi
local jpg_file="$SCITEX_WRITER_FIGURE_JPG_DIR/${base_name}.jpg"
if [ ! -f "$jpg_file" ]; then
echo_warning "Creating placeholder for missing figure: ${base_name}.jpg"
create_placeholder_jpg "$jpg_file" "$base_name"
fi
done
}
# EOF