#!/bin/bash
# doc/tool/latex - LaTeX support

set -e


######
# Native dependencies

doc_tool_latex_client_packages=( texlive )


######
# Config

doc_tool_latex_config_init() {
	lib_setting_vars latex_document_class latex_page_size
	lib_setting_vars --null latex_maketitle
	latex_document_class="article"
	latex_page_size="letterpaper"
}


######
# Generation


latex_opts() { [ -z "$1" ] || echo -n "[$1]"; }
latex_args() { [ -z "$1" ] || echo -n "{$1}"; }
latex_opts_and_args() { latex_opts "$2"; latex_args "$1"; }

latex_tag() { echo "\\$1$(latex_opts_and_args "$2" "$3")"; }
latex_begin() { echo "\\begin{$1}$(latex_opts_and_args "$2" "$3")"; }
latex_end() { echo "\\end{$1}"; }

latex_document() {
	local headfunc=$1
	local bodyfunc=$2
	shift 2

	local latex_maketitle=false

	latex_tag documentclass "$latex_document_class" "$latex_page_size"
	$headfunc "$@"
	latex_tag begin document
	! $latex_maketitle || latex_tag maketitle
	$bodyfunc "$@"
	latex_tag end document
}


######
# Preamble

latex_document_title() { latex_tag title "$1"; latex_maketitle=true; }
latex_document_author() { latex_tag author "$1"; latex_maketitle=true; }
latex_document_date() { latex_tag date "$1"; latex_maketitle=true; }
latex_document_date_today() { latex_document_date '\today'; }

latex_package() {
	min_args 1 "$@"
	echo "\\usepackage$(latex_opts_and_args "$@")"
}

latex_page_style() {
	local style=$1
	case "$style" in
	(empty|plain|headings|myheadings)
		;;
	(fancy|fancyplain)
		latex_package 'fancyhdr'
		;;
	(*)
		error "$style: unknown page style"
		;;
	esac
	echo "\\pagestyle{$style}"
}


######
# Content

latex_newpage() { latex_tag newpage; }

latex_part() { latex_tag part "$1"; }
latex_chapter() { latex_tag chapter "$1"; }
latex_section() { latex_tag section "$1"; }
latex_section1() { latex_tag subsection "$1"; }
latex_section2() { latex_tag subsubsection "$1"; }
latex_section3() { latex_tag paragraph "$1"; }
latex_section4() { latex_tag subparagraph "$1"; }


latex_tabular() {
	min_args 3 "$@"
	local __spec=$1
	local __hfunc=$2
	local __bfunc=$3
	shift 3
	latex_begin tabular "$__spec"
	$__hfunc "$@"
	for item in "$@"; do
		latex_tag hline
		$__bfunc "$item"
	done
	latex_end tabular
}

######
# Compilation

run_pdflatex() {
	run pdflatex "$1"
	run pdflatex "$1"
}

latex_pdf() {
	local name=$1
	local dir
	dir=$(dirname "$name")
	name=$(basename "$name")

	run_pushd "$dir"
	run_pdflatex "$name"
	run_popd
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:39 PDT 2017 by mcsh d14 v0.23.0.