10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaTeXiTのように数式だけのPDFを作るスクリプト

Last updated at Posted at 2014-10-10

余計な余白やページ番号のない,数式だけのPDFを作るためのスクリプトです.
Linux用のLaTeXiTの代替品として作成しました.

使い方

数式作成

latexit.sh 'e^{i\pi} + 1 = 0' >| eq1.pdf

-cオプションでLaTeXの処理系をデフォルトのpdflatexから変更できます.

latexit.sh -clualatex '速度\times 時間 = 距離' >| eq1.pdf

数式を標準入力から与えることもできます.

latexit.sh <<'EOF' >| eq1.pdf
a &= b \\
b &= c
EOF

PDFから数式を抽出

latexit.shによって作成されたPDFからは,もとの数式を取り出せます.

latexit.sh -p < eq1.pdf

数式は~/log/latexit.sh/にも保存されています.

latexit.sh

最新版はこちら

#!/bin/bash

# set -xv
set -o nounset
set -o errexit
set -o pipefail
set -o noclobber


readonly program_name="$(basename "${0}")"
usage_and_exit(){
   {
      cat <<EOF
# create a PDF

${program_name} [options] 'e^{i\pi} + 1 = 0' >| eq1.pdf
${program_name} [options] < eq1.tex >| eq1.pdf
# -h, --help: print help message
# -cLATEX, --command=LATEX: set LaTeX engine [pdflatex]

# extract a formula from a PDF

${program_name} -p < eq1.pdf
# -p, --print: print a LaTeX formula in a PDF file
EOF
   } > /dev/stderr
   exit "${1:-1}"
}


opts=$(
   getopt \
      --unquoted \
      --options hpc: \
      --longoptions help,print,command: \
      -- \
      "${@}"
)
set -- ${opts} # DO NOT quote.

while true
do
   case "${1}" in
      "-h" | "--help")
         usage_and_exit 0
         ;;
      "-p" | "--print")
         opt_print=true
         ;;
      "-c" | "--command")
         opt_command="${2}"
         shift
         ;;
      --)
         shift
         break
         ;;
      *)
         usage_and_exit 1
         ;;
   esac
   shift
done

if [[ "${opt_print:-false}" = true ]]; then
   pdftk - dump_data_utf8 |
   grep -A1 'InfoKey: '"${program_name}" |
   tail -n1 |
   sed -e 's/InfoValue: //' |
   base64 --decode
   exit
fi

readonly tmp_dir="$(mktemp -d)"
trap finalize EXIT
finalize(){
   rm -fr "${tmp_dir}"
}
cd "${tmp_dir}"

readonly latex="${opt_command:-pdflatex}"
readonly base_name=main
readonly tex_file="${base_name}".tex
readonly pdf_file="${base_name}".pdf
readonly log_dir="${HOME}"/log/"${program_name}"
mkdir -p "${log_dir}"
readonly log_file="${log_dir}"/"$(date +'%FT%T.%N%z')".tex
{
   if [[ $# -ne 0 ]]; then
      echo "$@"
   else
      cat
   fi
} > "${log_file}"

{
   cat <<EOF
%\RequirePackage[l2tabu, orthodox]{nag}
EOF
   if [[ "${latex}" = "lualatex" ]]; then
      cat <<EOF
\documentclass{ltjsarticle}
\usepackage[ipaex, deluxe, expert]{luatexja-preset}
EOF
   else
      cat <<EOF
\documentclass{article}
EOF
   fi
cat <<EOF
\usepackage[a0paper, landscape, margin=10mm]{geometry}
\usepackage[usenames]{color}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{mathrsfs}
\usepackage{siunitx}
\usepackage{bm}

\DeclareMathOperator{\D}{\partial}
\newcommand{\dd}[1]{\,d#1}
\newcommand{\f}[2]{\frac{#1}{#2}}

\pagenumbering{gobble}

\begin{document}
\begin{align*}
EOF
   cat "${log_file}"
   cat <<EOF
\end{align*}
\end{document}
EOF
} > "${tex_file}"

"${latex}" "${tex_file}" > /dev/stderr
pdfcrop --margins=1 "${pdf_file}" cropped.pdf > /dev/stderr
{
   echo 'InfoKey: '"${program_name}"
   echo InfoValue: "$(base64 "${log_file}" | tr -d '\n')"
} | pdftk cropped.pdf update_info_utf8 - output /dev/stdout

依存関係

  • PDFLaTeX(-cオプションで任意の処理系を指定できる)
  • PDFCrop
    • sudo port install texlive-bin-extra
  • PDFtk
    • sudo port install pdftk

License

GPL version 3.

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?