Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
281 views
ubuntu2404
# LaTeXmk configuration for Computational Physics Template
# Optimized for PythonTeX compilation with quantum mechanics simulations
#
# This configuration enables:
# - Automatic PythonTeX execution for computational content
# - Shell escape for secure code execution
# - Efficient caching and dependency tracking
# - Publication-quality PDF generation

# Set PDF generation mode
$pdf_mode = 1;

# Use pdflatex as the PDF engine
$pdflatex = 'pdflatex -interaction=nonstopmode -shell-escape %O %S';

# Output directory for build artifacts
$out_dir = 'build';

# Enable shell escape for PythonTeX (REQUIRED for computational content)
# Security note: Only run trusted LaTeX documents with -shell-escape
$latex = 'pdflatex -interaction=nonstopmode -shell-escape %O %S';

# PythonTeX configuration
# Custom dependency and build rules for PythonTeX integration
add_cus_dep('pytxcode', 'tex', 0, 'pythontex');

sub pythontex {
    # PythonTeX dependency handler
    # This sub ensures PythonTeX is run when .pytxcode files are updated
    my $base = $_[0];
    my $dir = $_[1];

    # Change to output directory for PythonTeX execution
    my $return_code = 0;

    # Run PythonTeX with Python 3
    if (-e "$out_dir/$base.pytxcode") {
        pushd($out_dir);
        $return_code = system("pythontex --interpreter python:python3 $base.pytxcode");
        popd();

        # Check for successful execution
        if ($return_code != 0) {
            warn "PythonTeX execution failed with return code: $return_code\n";
        }
    }

    return $return_code;
}

# File extensions to monitor for changes
@default_files = ('*.tex', '*.bib', 'code/*.py');

# Additional file types to clean
$clean_ext = 'pytxcode pytxmcr pytxpyg';

# Clean full PythonTeX directory
$clean_full_ext = 'pytxcode pytxmcr pytxpyg';

# Add Python files in code/ directory as dependencies
# This ensures recompilation when simulation code changes
@cus_dep_list = (
    'py tex 0 py_to_tex'
);

sub py_to_tex {
    # Monitor Python files for changes
    return 0;  # No conversion needed, just dependency tracking
}

# Force regeneration if Python files change
# Add all .py files in code/ directory to dependencies
opendir(my $dh, 'code/') or die "Cannot open code/ directory: $!";
my @py_files = grep { /\.py$/ && -f "code/$_" } readdir($dh);
closedir($dh);

foreach my $py_file (@py_files) {
    push @file_not_found, "code/$py_file";
}

# Bibliography processing (if needed)
$bibtex_use = 2;  # Use bibtex when .bib files present
$biber = 'biber %O --input-directory=%BIBER_DIR %B';

# Index processing (if needed)
$makeindex = 'makeindex %O -o %D %S';

# Continuous mode settings
$sleep_time = 2;  # Check for changes every 2 seconds
$preview_continuous_mode = 1;

# Success/failure reporting
$success_cmd = 'echo "✓ Computational physics PDF generated successfully!"';
$failure_cmd = 'echo "✗ Compilation failed - check output for errors"';

# File monitoring for computational content
# Watch for changes in:
# - Main LaTeX file
# - All Python simulation files
# - Bibliography files
# - Asset files

push @file_not_found, glob("code/*.py");
push @file_not_found, glob("assets/*");

# Progress reporting
$silent = 0;  # Show compilation progress

# Advanced PythonTeX integration
# Ensure proper handling of computational outputs

# Custom rule for assets generated by PythonTeX
add_cus_dep('py', 'pdf', 0, 'py_to_assets');

sub py_to_assets {
    # Handle assets generated by Python code
    # This ensures figures are regenerated when code changes
    my $base = $_[0];
    my $dir = $_[1];

    # Check if assets directory exists
    if (!-d "assets") {
        mkdir("assets") or warn "Cannot create assets directory: $!\n";
    }

    return 0;
}

# Environment setup for computational physics
# Set environment variables for Python execution
$ENV{'PYTHONPATH'} = 'code:' . ($ENV{'PYTHONPATH'} || '');
$ENV{'MPLBACKEND'} = 'Agg';  # Use non-interactive matplotlib backend

# Memory and performance settings for large computations
$max_repeat = 3;  # Maximum compilation attempts

# Post-processing hooks
# Actions to perform after successful compilation
sub cleanup_pytex_files {
    # Clean up temporary PythonTeX files while preserving outputs
    unlink glob("$out_dir/pythontex-files-*/*.stderr");
    unlink glob("$out_dir/pythontex-files-*/*.stdout");
}

# Register cleanup function
$cleanup_fdb = 1;
$cleanup_mode = 1;

# Specific settings for computational physics content
# These ensure optimal compilation for scientific documents

# Enable SyncTeX for editor integration
$pdflatex = 'pdflatex -interaction=nonstopmode -shell-escape -synctex=1 %O %S';

# Increase memory limits for large documents with many figures
$ENV{'max_print_line'} = '1000';
$ENV{'error_line'} = '254';
$ENV{'half_error_line'} = '238';

# Custom warnings for computational physics template
sub check_computational_requirements {
    # Verify computational requirements are met
    my $python_check = system("python3 -c 'import numpy, scipy, matplotlib' 2>/dev/null");
    if ($python_check != 0) {
        warn "Warning: Required Python packages not available\n";
        warn "Install with: pip install numpy scipy matplotlib\n";
    }

    my $pythontex_check = system("which pythontex >/dev/null 2>&1");
    if ($pythontex_check != 0) {
        warn "Warning: PythonTeX not found in PATH\n";
        warn "Install with: pip install pythontex\n";
    }
}

# Run checks before compilation
&check_computational_requirements();

# Document metadata for SEO and indexing
$ENV{'LATEX_TEMPLATE_TYPE'} = 'computational-physics';
$ENV{'LATEX_KEYWORDS'} = 'quantum mechanics,statistical mechanics,condensed matter,monte carlo,ising model';

print "LaTeXmk configured for Computational Physics Template\n";
print "Features: PythonTeX integration, automatic recompilation, asset management\n";
print "Requirements: pdflatex with -shell-escape, Python 3, NumPy, SciPy, Matplotlib\n";