0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

inputで分割しながらlatexdiffで差分をとりつつgitで管理

Last updated at Posted at 2025-11-07

はじめに

以前投稿したgitでlatex管理を運用してしばらく経過した.
論文本体が長くなるにつれ,コンパイルに時間がかかるため,ファイルを分割するようになった.
はじめは個別ファイル単体でのコンパイルを可能にするため,subfilesパッケージを使用していた.
しかしいろいろ問題が発生したため,以下のような解決策を模索した.
そこで今回は

  • \inputを使いつつ,個別ファイル(セクション)ごとにコンパイル可能
  • latexdiffで差分取得
  • gitでバージョン管理

することを目指す.

\inputでの個別ファイルごとのコンパイル

通常,\inputでセクションファイルを取り込む際はdocumentclass等を宣言できないため,単体でのコンパイルはできない.
そこで条件分岐を追加して解決する.

main.tex
\documentclass{article}
\def\MAINFILENAME{main} % mainのファイルであることを示すフラグ
\input{preamble.tex}

\title{}
\author{tonta}
\date{\today}

\begin{document}

\input{intro}

\end{document}
intro.tex
\ifdefined\MAINFILENAME\else % \mainfilenameが定義されなければdocumentclassを宣言するif文
\documentclass{article}
\input{preamble.tex}
\begin{document}
\fi

\section{Introduction}
hogehogehoge

\ifdefined\MAINFILENAME\else
%\bibliographystyle{}
%\bibliography{}
\end{document}
\fi

main.texをコンパイルするとき,documentclassの宣言が重複しないように\MAINFILENAMEを定義して回避する.
\end{document}を含む部分に\bibliographystyle{},\bibliography{}を入れ込むことで,セクション単体でのコンパイル時にも引用を表示できる.

latexdiffで差分取得

通常のlatexdiffでは\inputや\subfileで取り込むファイル内の変更までは認識できない.
また\subfileで取り込む場合に --flatten をオプションとして入れても,コンパイルがうまくいかなかった.
加えて

latexdiff-vc --git --flatten -r [branch name] main.tex

もコンパイルできなかった.

そこで以下のような方法をとる.

  1. 差分をとりたい過去ファイル(old-main.texとする)を解凍する
  2. 現行ファイルと過去ファイルの差分をlatexdiffで生成する
    (latexdiffであれば生成できた)
  3. 差分ファイルをコンパイル

gitに関する操作をすべてgit bash上で行っているため,以下のような.shファイルで解凍と生成を行う.

difftex.sh
if [ $# -lt 3 ]; then
  echo "Usage: $0 oldbranch newbranch targetfile"
  exit 1
fi

old=$1
new=$2
file=$3

echo "Comparing $file between $old (old) and $new (new)..."

# 一時ディレクトリを作成
mkdir -p tmp_$old tmp_$new

# 各ブランチをアーカイブして展開
git archive $old --format=tar | tar -x -C tmp_$old
git archive $new --format=tar | tar -x -C tmp_$new

# 差分texファイルを作成
latexdiff --flatten tmp_$old/$file tmp_$new/$file > diff.tex

# 一時ディレクトリを削除
rm -rf tmp_$old tmp_$new

echo "✅ diff.tex generated successfully."

これをmain.texがあるディレクトリに保存する.

実際の手順

  1. 以下を実行.これはスクリプトに実行権限を与えるコマンドで,毎回の作業に必要なく1回限りでよい.
    chmod +x difftex.sh 
    
  2. difftex.shで差分出力ファイルを生成する
    ./difftex.sh [old branch] [new branch] [targetfile]
    
    Git のオブジェクト名(ブランチ名、タグ名、コミットID)のすべてに対応しているのでブランチ名でなくても可.
  3. main.texがあるディレクトリにdiff.texが生成される
  4. diff.texをコンパイルして差分ファイルを生成する

カスタマイズ

# 差分texファイルを作成
latexdiff --flatten tmp_$old/$file tmp_$new/$file > diff.tex

"diff.tex"が生成されるファイル名なのでここは任意のものに変更可能

最後に

生成されるファイルのコード自体は--flattenによって\inputをすべて呑み込んでいるので,ものすごく汚いが重要なのはそこから出来上がるpdfなので,無視する.
チャッピーありがとう.

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?