作曲・指導・C言語・Linux

金沢音楽制作

金沢音楽制作では、楽曲・楽譜の制作と、作曲や写譜などレッスンを行っています。

tex-open

LaTeXで書かれたファイルをpdfに変換して、pdfが関連付けられたGUIアプリで開きます。ついでにaux、log、dviといった不要なファイルも削除します。

環境:TeX Live、bash 5.1.16

実行画面

対象のTeXファイルを引数に指定します。問題がなければコンパイルされて、自動的にpdfが開きます。Linuxのxdg-openかmacOSのopenかを自動で判別してくれます。

$ ./tex-open.sh profile.tex
$

tex-openのコード

#!/bin/bash

declare -ri argc="$#"
declare -r  file_name="$1"
declare -r  base_name="${1%.*}"
declare -r  script_name=${0##*/}

fn_main()
{
  fn_is_argment || fn_show_using && exit 1
  fn_is_tex     || fn_show_using && exit 1

  # errorstopmode scrollmode nonstopmode \batchmode
  ptex2pdf -l -interaction=nonstopmode ${file_name}

  if [ $? -ne 0 ]; then
    echo "Compile Error!!!"
    exit 1
  fi 

  fn_open_file
  fn_delete_files
}

fn_is_argment()
{
  if [ ${argc} -ne 1 ]; then
    echo "Argument Error!!!"
    return 1
  fi
}

fn_is_tex()
{
  grep -m 1 '\\documentclass\[.*\]{.*}' ${file_name}
  if [ $? -ne 0 ]; then
    echo "${file_name} is not tex"
    return 1
  fi
}

fn_delete_files()
{
  local -a extensions=(aux log dvi out)

  for ext in ${extensions[*]}; do
    local delete_file=$(echo "${base_name}.${ext}")

    if [ -f ${delete_file} ]; then
      rm ${delete_file} > /dev/null 2>&1
    fi
  done
}

fn_open_file()
{
  local open_command=$(which xdg-open || which open)
  local -r target_file="${base_name}.pdf"

  eval "${open_command} ${target_file}"
}

fn_show_using()
{
  echo "Using: $ ${script_name} [filename].tex"
}

fn_main

更新情報

  • 作成日:2020-07-03
  • 作成日:2022-02-11
  • 作成日:2022-05-02