7
4

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.

原文と翻訳を並べたLaTeXを書く

Last updated at Posted at 2018-12-08

(追記01) Wordで提出だった.死にたい.
(追記02) 古坂大魔王の字間違えてた.死にたい.

動機

学校の課題でこんな課題が出た.

各文章を日本語に翻訳する.なお、英語と日本語を1文ずつ対比できるように記述すること.

めんどくさい.英語訳すだけでもめんどくさいのに並べて書くとかさらにめんどくさい.
ええい自動化してやれ.(期待してる人はいないと思うが,英訳ではなく並べて書くところを)

対訳ファイルを作る

ここは人力.下記のファイルを作る.行ごとに対応させること.

ppap_en.txt
I have a Pen.
I have an Apple.
uhhhhhhh, Apple-Pen!

I have a Pen.
I have a Pineapple.
uhhhhhhh, Pineapple-Pen!

Apple-Pen...
Pineapple-Pen...
uhhhhhhh, Pen-Pineapple-Apple-Pen!
ppap_ja.txt
私はペンを持っています.
私はリンゴを持っています.
んああ,リンゴ-ペン!

私はペンを持っています.
私はパイナップルを持っています.
んああ,パイナップル-ペン!

リンゴ-ペン…
パイナップル-ペン…
んああ,ペン-パイナップル-リンゴ-ペン!

LaTeX形式に変換

multicolpar.styというスタイルファイルを使う前提.使い方はここを参照のこと.

下記のPythonコードが_latex.txtっていうサフィックスのついたファイルを作成してくれる.ただ行をなめてるだけだけど.

translate_in_latex.py
from os.path import join

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='./src')
parser.add_argument('--prefix', type=str, default='ppap')
flags = parser.parse_args()

if __name__ == '__main__':
    file_en = join(flags.data_dir, flags.prefix + '_en.txt')
    file_ja = join(flags.data_dir, flags.prefix + '_ja.txt')

    with open(file_en) as f:
        lines_en = f.readlines()
    with open(file_ja) as f:
        lines_ja = f.readlines()

    file_out = join(flags.data_dir, flags.prefix + '_latex.txt')
    with open(file_out, mode='w') as f:
        for i, (line_en, line_ja) in enumerate(zip(lines_en, lines_ja)):
            if line_en == '\n' or line_ja == '\n':
                print('[line {}] blank row was skipped.'.format(i+1))
            else:
                block = ['\\begin{multicolpar}{2}\n',
                         line_en,
                         '\n',
                         line_ja,
                         '\\end{multicolpar}\n',
                         '\n']
                for line in block:
                    f.write(line)
    print('[!] file {} was generated!'.format(file_out))

生成されたファイルがこれ.

ppap_latex.txt
\begin{multicolpar}{2}
I have a Pen.

私はペンを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
I have an Apple.

私はリンゴを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Apple-Pen!

んああ,リンゴ-ペン!
\end{multicolpar}

\begin{multicolpar}{2}
I have a Pen.

私はペンを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
I have a Pineapple.

私はパイナップルを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Pineapple-Pen!

んああ,パイナップル-ペン!
\end{multicolpar}

\begin{multicolpar}{2}
Apple-Pen...

リンゴ-ペン…
\end{multicolpar}

\begin{multicolpar}{2}
Pineapple-Pen...

パイナップル-ペン…
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Pen-Pineapple-Apple-Pen!

んああ,ペン-パイナップル-リンゴ-ペン!
\end{multicolpar}

実際にLaTeXでタイプセットしてみる

さっきも言ったけど,multicolpar.styを使うのでプリアンブルに書くのをお忘れなく.
ppap_latex.txtの中身をdocument環境の中に突っ込めば終了.

ppap.tex

\documentclass[11pt]{jarticle} 
\usepackage{otf}
\usepackage{multicolpar}


\title{¥LARGE Pen-Pineapple-Apple-Pen} 
\author{¥normalsize 古坂学部 大魔王学科 ピコ太郎¥¥指導教員 ジャスティンビーバー} 
\date{¥today}

\pagestyle{empty}

\begin{document}
\maketitle

\begin{multicolpar}{2}
I have a Pen.

私はペンを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
I have an Apple.

私はリンゴを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Apple-Pen!

んああ,リンゴ-ペン!
\end{multicolpar}

\begin{multicolpar}{2}
I have a Pen.

私はペンを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
I have a Pineapple.

私はパイナップルを持っています.
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Pineapple-Pen!

んああ,パイナップル-ペン!
\end{multicolpar}

\begin{multicolpar}{2}
Apple-Pen...

リンゴ-ペン…
\end{multicolpar}

\begin{multicolpar}{2}
Pineapple-Pen...

パイナップル-ペン…
\end{multicolpar}

\begin{multicolpar}{2}
uhhhhhhh, Pen-Pineapple-Apple-Pen!

んああ,ペン-パイナップル-リンゴ-ペン!
\end{multicolpar}
\end{document}

下記出来上がったファイル.できた.
ppap.png

まとめ

こんなことしてるから課題が進まない.

参考

「対訳スタイルで書く」 - http://www.ic.daito.ac.jp/~mizutani/tex/doc/translate/translate_sample.pdf
「Pythonでファイルの読み込み、書き込み(作成・追記)」 - https://note.nkmk.me/python-file-io-open-with/
「PPAP(Pen-Pineapple-Apple-Pen Official)ペンパイナッポーアッポーペン/PIKOTARO(ピコ太郎)」 - https://www.youtube.com/watch?v=0E00Zuayv9Q
「古坂大魔王 - Wikipedia」 - https://ja.wikipedia.org/wiki/%E5%8F%A4%E5%9D%82%E5%A4%A7%E9%AD%94%E7%8E%8B

補足

下記みたいなファイル構造で作ってるよ.なんか不備があったらすまん.

[root]
  | - [src]
  |    | - ppap_en.txt
  |    | - ppap_ja.txt
  |    | - ppap_latex.txt
  | - translate_in_latex.py
  | - multicolpar.sty
  | - ppap.tex
  | - ppap.pdf # 出力ファイル
7
4
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
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?