Atom 上の LaTeX 数式環境で、Snippets が出ない
Atom で LaTeX をやろうとしたら、数式環境で Snippets がうまく機能しないことが判明。
いろいろ調べて、原因と一応の解決策が得られたのでメモ。
※ この記事は古いので、こちらを参照(2017/12/26)
解決策
「ctrl-,」で設定を開き、「Packages」のところで「language-latex」を検索してクリック。
「View Code」を押すと、新しい Atom のウィンドウで「language-latex」のフォルダが開かれる。
以下のサイトを参考に該当ファイルの該当箇所を修正。
github.com
原因
調べたところ、原因は恐らく次の2点であることが分かった。
- パッケージ「language-latex」の仕様:
- 数式環境内は string として認識される
- パッケージ「autocomplete-snippets」の仕様:
- comment や string の所にカーソルがあると、Snippets などの補完候補を出さない
これらは次のページに書かれている。github.com
まとめ
パッケージをダイレクトにいじっているので、アップデートの際や、他の人と Snippets を共有する際に問題が発生しそう。
テキストモードと数式モードの違いがわかりづらくなるのもあるし・・・。
と言っても、スタイルシートで文字に色づけすればいいだけだから、大した問題ではないけど。
とりあえず、もっと便利な方法が見つかったら更新する予定。
てか、本当はパッケージ作って対応したかったんだけど、難しくて挫折中・・・。
Atom の LaTeX 用 Snippets(個人用)
パッケージ「language-latex」の Snippets は、次のサイトから取得していると思われる。
github.com自分用として、これに細かい修正を加えたかったので、「File」→「Snippets」からファイル「snippets.cson」を開いて、そこに下記の内容を追加。
################################## # My snippets for LaTeX # # Last modified time: 2017/10/06 # ################################## '.text.tex.latex': # preamble and package/class file '\\documentclass{article}...': 'prefix': 'article' 'body': '\\\\documentclass[a4paper, 12pt]{article}\n$2\n\\\\begin{document}\n\t\n\\\\end{document}' '\\documentclass{jsarticle}...': 'prefix': 'jsarticle' 'body': '\\\\documentclass[a4paper, 12pt]{jsarticle}\n$2\n\\\\begin{document}\n\t\n\\\\end{document}' '\\usepackage{}': 'prefix': 'usepackage' 'body': '\\\\usepackage{$1}' # \begin{}...\end{} '\\begin{center}...\\end{center}': 'prefix': 'center' 'body': '\\\\begin{center}\n\t${2:% text}\n\\\\end{center}' # Document level '\\chapter{}': 'prefix': 'chapter' 'body': '%---------------------------------------------------------------\n% Chapter ${1:1}\n%---------------------------------------------------------------\n\\\\chapter{${2:chapter name}}\n\\\\label{chap: ${2:chapter label}}\n\t$0' '\\section{}': 'prefix': 'section' 'body': '%-----------------------------------------------------------\n% Section ${1:1}\n%-----------------------------------------------------------\n\\\\section{${2:section name}}\n\\\\label{sec: ${2:section label}}\n\t$0' '\\subsection{}': 'prefix': 'subsection' 'body': '%-------------------------------------------------------\n% Subsection ${1:1.1}\n%-------------------------------------------------------\n\\\\subsection{${2:subsection name}}\n\\\\label{sub: ${2:subsection label}}\n\t$0' '\\subsubsection{}': 'prefix': 'subsubsection' 'body': '%---------------------------------------------------\n% Subsubsection ${1:1.1.1}\n%---------------------------------------------------\n\\\\subsubsection{${2:subsubsection name}}\n\\\\label{subs: ${2:subsubsection label}}\n\t$0' # environment '\\begin{center}...\\end{center}': 'prefix': 'center' 'body': '\\\\begin{center}\n\t${2:% text}\n\\\\end{center}' # amsmath '\\usepackage{amsmath}': 'prefix': 'amsmath' 'body': '\\\\usepackage{amsmath}' '\\begin{equation}...\\end{equation}': 'prefix': 'Equation' 'body': '\\\\begin{equation}\n\t${2:% equation}\n\\\\label{eq: ${1:ref_name}}\n\\\\end{equation}' '\\begin{equation*}...\\end{equation*}': 'prefix': 'equation' 'body': '\\\\begin{equation*}\n\t${1:% equation}\n\\\\end{equation*}' '\\begin{align}...\\end{align}': 'prefix': 'Align' 'body': '\\\\begin{align}\n\t${2:% equation}\n\\\\label{eq: ${1:ref_name}}\n\\\\end{align}' '\\begin{align*}...\\end{align*}': 'prefix': 'align' 'body': '\\\\begin{align*}\n\t${1:% equation}\n\\\\end{align*}' # amsthm '\\usepackage{amsthm}': 'prefix': 'amsthm' 'body': '% Settings: Theorem environment\n\\\\usepackage{amsthm}\n\t\\\\newtheorem{definition}{Definition}[section]\n\t\\\\newtheorem{theorem}{Theorem}[section]\n\t\\\\newtheorem{lemma}[theorem]{Lemma}\n\t\\\\newtheorem{proposition}[theorem]{Proposition}\n\t\\\\newtheorem{corollary}[theorem]{Corollary}\n\t\\\\newtheorem{remark}{Remark}[section]' '\\theoremstyle{}': 'prefix': 'theoremstyle' 'body': '\\\\theoremstyle{${1:stylename}}\t% stylename = plain, definition, remark' '\\newtheoremstyle{}': 'prefix': 'newtheoremstyle' 'body': ''' \\\\newtheoremstyle{${1:stylename}} % name of the style to be used \t{${2:spaceabove}} % measure of space to leave above the theorem. E.g.: 3pt \t{${3:spacebelow}} % measure of space to leave below the theorem. E.g.: 3pt \t{${4:bodyfont}} % name of font to use in the body of the theorem \t{${5:indent}} % measure of space to indent \t{${6:headfont}} % name of head font \t{${7:headpunctuation}} % punctuation between head and body \t{${8:headspace}} % space after theorem head; " " = normal interword space \t{${9:headspec}} % Manually specify head. ''' '\\newtheorem{}{}': 'prefix': 'newtheorem' 'body': '\\\\newtheorem{${1:name}}{${2:Printed output}}' '\\newtheorem{}{}[numberby]': 'prefix': 'newtheorem_num' 'body': '\\\\newtheorem{${1:env_name}}{${2:output_name}}[${3:numberby}]' '\\newtheorem{}[counter]{}': 'prefix': 'newtheorem_count' 'body': '\\\\newtheorem{${1:env_name}}[${2:counter}]{${3:output_name}}' '\\begin{definition}...\\end{definition}': 'prefix': 'definition' 'body': '\\\\begin{definition}\n\t${2:% text}\n\\\\label{def: ${1:ref_name}}\n\\\\end{definition}' '\\begin{theorem}...\\end{theorem}': 'prefix': 'theorem' 'body': '\\\\begin{theorem}\n\t${2:% text}\n\\\\label{thm: ${1:ref_name}}\n\\\\end{theorem}' '\\begin{lemma}...\\end{lemma}': 'prefix': 'lemma' 'body': '\\\\begin{lemma}\n\t${2:% text}\n\\\\label{lem: ${1:ref_name}}\n\\\\end{lemma}' '\\begin{proposition}...\\end{proposition}': 'prefix': 'proposition' 'body': '\\\\begin{proposition}\n\t${2:% text}\n\\\\label{prop: ${1:ref_name}}\n\\\\end{proposition}' '\\begin{corollary}...\\end{corollary}': 'prefix': 'corollary' 'body': '\\\\begin{corollary}\n\t${2:% text}\n\\\\label{cor: ${1:ref_name}}\n\\\\end{corollary}' '\\begin{remark}...\\end{remark}': 'prefix': 'remark' 'body': '\\\\begin{remark}\n\t${2:% text}\n\\\\label{rem: ${1:ref_name}}\n\\\\end{remark}' '\\begin{problem}...\\end{problem}': 'prefix': 'problem' 'body': '\\\\begin{problem}\n\t${2:% text}\n\\\\label{prob: ${1:ref_name}}\n\\\\end{problem}' '\\begin{example}...\\end{example}': 'prefix': 'example' 'body': '\\\\begin{example}\n\t${2:% text}\n\\\\label{e.g.: ${1:ref_name}}\n\\\\end{example}' '\\begin{proof}...\\end{proof}': 'prefix': 'proof' 'body': '\\\\begin{proof}\n\t$1\n\\\\end{proof}' # math environment '.text.tex.latex .string.other.math': # fractions '\\frac': 'prefix': 'frac' 'body': '\\\\frac{$1}{$2} $0' 'd/dx': 'prefix': 'differential operator' 'body': '\\\\frac{d$2}{d${1:x}$2} $0' 'd/dx: partial': 'prefix': 'partially differential operator' 'body': '\\\\frac{\\\\partial$2}{\\\\partial ${1:x}$2} $0' # variable-sized symbols '\\sum': 'prefix': 'sum' 'body': '\\\\sum_{$1}^{$2} $0' '\\prod': 'prefix': 'product' 'body': '\\\\prod_{$1}^{$2} $0' '\\bigcap': 'prefix': 'bigcap' 'body': '\\\\bigcap_{$1}^{$2} $0' '\\bigcup': 'prefix': 'bigcup' 'body': '\\\\bigcup_{$1}^{$2} $0' '\\int': 'prefix': 'int' 'body': '\\\\int_{$1}^{$2} ${4:f(x)} \\\\, d${3:x} $0' # log-like Symbols '\\det()': 'prefix': 'det' 'body': '\\\\det \\\\left ( $1 \\\\right ) $0' '\\exp()': 'prefix': 'exp' 'body': '\\\\exp \\\\left ( $1 \\\\right ) $0' '\\lim': 'prefix': 'lim' 'body': '\\\\lim_{$1} $0' '\\liminf_{}': 'prefix': 'liminf' 'body': '\\\\liminf_{$1} $0' '\\limsup_{}': 'prefix': 'limsup' 'body': '\\\\limsup_{$1} $0' '\\max': 'prefix': 'max' 'body': '\\\\max_{$1} $0' '\\min': 'prefix': 'min' 'body': '\\\\min_{$1} $0' # brackets '\\left \\right': 'prefix': 'LeftRight' 'body': '\\\\left ${1:<} $3 \\\\right ${2:>} $0' '()': 'prefix': 'brackets.soft' 'body': '\\\\left ( $1 \\\\right ) $0' '{}': 'prefix': 'brackets.curly' 'body': '\\\\left \\\\{ $1 \\\\right \\\\} $0' '[]': 'prefix': 'brackets.hard' 'body': '\\\\left [ $1 \\\\right ] $0' '<>': 'prefix': 'brackets' 'body': '\\\\left < $1 \\\\right > $0' 'angular <>': 'prefix': 'brackets.angular' 'body': '\\\\left \\\\langle $1 \\\\right \\\\rangle $0' '| |': 'prefix': 'absolute value' 'body': '\\\\left | $1 \\\\right | $0' '|| ||': 'prefix': 'norm' 'body': '\\\\left \\\\| $1 \\\\right \\\\| $0' '||...||_{}': 'prefix': 'Norm' 'body': '\\\\left \\\\| $2 \\\\right \\\\|_{$1} $0' # other '\\begin{matrix}...\\end{matrix}': 'prefix': 'matrix' 'body': '\\\\begin{${1:p/b/v/V/B}matrix}\n\t$2\n\\\\end{${1:p/b/v/V/B}matrix}$0' '\\begin{matrix}...\\end{matrix}': 'prefix': 'SmallMatrix' 'body': '\\\\begin{smallmatrix} $1 \\\\end{smallmatrix} $0'
Atom のC言語用 Snippets(個人用)
コア・パッケージ「language-c」の Snippets は、次のサイトから取得している。
github.com自分用に細かい修正を加えたかったので、「File」→「Snippets」からファイル「snippets.cson」を開いて、そこに下記の内容を追加。
(下記をコピーする場合、カーソルを置くと現れる「select」ボタンを押すと楽)
################################## # My snippets for C language # # Last modified time: 2017/09/30 # ################################## '.source.c, .source.cpp, .source.objc, .source.objcpp': # header 'Header Include-Guard': 'prefix': 'IncludeGuard' 'body': '#ifndef ${1:SYMBOL} /* beginning of include guard ${1:SYMBOL} */\n#define ${1:SYMBOL}\n\n${2}\n\n#endif /* end of include guard: $1 */\n' '#include <>': 'prefix': 'Inc' 'body': '#include <${1:stdio.h}>' '#include ""': 'prefix': 'inc' 'body': '#include "${1:current.h}"' 'Function-like Macro': 'prefix': 'MacroFunc' 'body': '#define ${1:f}(${2:x}) do { ${3:macro} } while (0)' 'typedef enum': 'prefix': 'enum' 'body': 'typedef enum { $2 } ${1:Name};' 'typedef struct': 'prefix': 'struct' 'body': 'typedef struct {\n\t${2:int };\n} ${1:Name};' 'linear list': 'prefix': 'llist' 'body': 'typedef struct ${1:__node} {\n\t${3:int val};\n\tstruct ${1:__node} *next;\n} ${2:Node};' 'function': 'prefix': 'func' 'body': '${1:void} ${2:functionName}(${3:int parameter}) {\n\t${4:/* function body */}\n}' 'main()': 'prefix': 'main' 'body': 'int main(int argc, char const *argv[]) {\n\t${1:/* code */}\n\n\treturn 0;\n}' # stdio.h 'printf()': 'prefix': 'printf' 'body': 'printf("$1", ${2:var});' 'scanf()': 'prefix': 'scanf' 'body': 'scanf("%${1:s}", ${2:ptr});' 'putchar()': 'prefix': 'putchar' 'body': '''putchar('$1');''' 'puts()': 'prefix': 'puts' 'body': 'puts("$1");' 'Switch Statement': 'prefix': 'switch' 'body': 'switch (${1:/* expression */}) {\n\tcase ${2:/* value */} :\n\t\t${3:/* code */}\n}' 'case :': 'prefix': 'case' 'body': 'case ${1:value} :\n\t${2:/* code */}' 'Do Loop': 'prefix': 'do' 'body': 'do {\n\t${2:/* code */}\n} while (${1:/* code */});' 'While Loop': 'prefix': 'while' 'body': 'while (${1:/* code */}) {\n\t${2:/* code */}\n}' 'For Loop - customize': 'prefix': 'For' 'body': 'for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:count}; ${2:i}${5:++}) {\n\t${6:/* code */}\n}' 'For Loop': 'prefix': 'for' 'body': 'for (size_t ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}++) {\n\t${3:/* code */}\n}' 'sizeof()': 'prefix': 'sizeof' 'body': 'sizeof(${1:int})' 'open file': 'prefix': 'fopen' 'body': '''if ((${1:fp} = fopen(${2:filename}, "${3:w}")) == NULL) {\n\tfprintf(stderr, "We have failed to open file \\\\"%s\\\\".\\\\n", ${2:filename});\n} else {\n\t${4:/* code */}\n\t\n\tfclose(${1:fp});\n}''' # stdlib.h 'rand()': 'prefix': 'rand' 'body': 'rand()' 'srand()': 'prefix': 'srand' 'body': 'srand(${1:(unsigned)time(NULL)});' 'calloc() - initialize': 'prefix': 'Calloc' 'body': '${1:int} *${2:ptr} = (${1:int} *)calloc(${3:num_arry}, sizeof(${1:int}))\n\nif (() == NULL) {\n\t${4:fprintf(stderr, "Failed to allocate memory.\\\\n")};\n} else {\n\t${5:/* code */}\n\t\n\tfree(${2:ptr});\n}' 'calloc()': 'prefix': 'calloc' 'body': '''/* allocate memory '${1:ptr}' */\nif ((${1:ptr} = (${2:int} *)calloc(${3:num_arry}, sizeof(${2:int}))) == NULL) {\n\tfprintf(stderr, "Failed to allocate memory '${1:ptr}'.\\\\n");\n} else {\n\t${5:/* code */}\n\t\n\tfree(${1:ptr});\n}''' 'bsearch()': 'prefix': 'bsearch' 'body': '''/* binary search '${1:sorted_array}' for '${2:key}' */\nif ((${3:res} = bsearch(&${2:key}, ${1:sorted_array}, ${4:nmemb}, sizeof(${5:int}), (int (*)(const void *, const void *))${6:compar})) == NULL) {\n\t${7:puts("We have failed to binary search.");}\n} else {\t/* Success: ${1:sorted_array}[(int)(${3:res} - ${1:sorted_array})] = ${2:key} */\n\t${8:/* code */}\n}''' # string.h 'strcmp()': 'prefix': 'strcmp' 'body': 'strcmp(${1:s1}, ${2:s2})'
「Atom」でのC言語学習環境を整える
前置き
エディタ「Atom」をインストールしてから、C言語の学習環境を整えるのに、随分手間取ってしまった。
以下、直面したトラブルや、調べるのに時間を費やしてしまったこと(解決済)。
- 「Linter」が上手く機能しない
- 「gpp-compiler」で文字化け&データ消失
- パッケージを導入してはみたものの、自動補完が自分好みでない
- 自作した自動補完が上書きできない
- 「//」ではなく、こっちのコメントアウト「/* */」を手軽に打てるようにしたい
- ソースコードを書いたときに、ヘッダの部分を自動でつけたい
これらに4、5日吸い取られたけど、一応全て解消できたので、まあ良しとしよう。
次回以降の手間を省くため、インストール後の手順を以下に記した。
環境
OS: Windows 10
システム: 64 ビット オペレーティング システム、x64 ベース プロセッサ
Atom: 1.19.5、x64
インストール後の手順
1.「Welcome Guide」をオフにする
- 「■ Show Welcome Guide when opening Atom」のチェックを外す
ついでに、開かれているタブ「Telemery Consent」で、YesかNoの選択。
入力パターンを自動で収集されるのが嫌なら、No。
特に問題ないなら、Yesといった具合。
2.パッケージのインストール
- 「Ctrl-,」を入力すると、「Settings」のタブが開けるので、「Install」を選択。
- 「Search packages」と書かれている検索Boxで、下記を検索してインストール。
日本語のメニューにしたい場合は、これもインストール(ここではインストールしていない)
3.各種設定
- 「Editor」の「Show Indent Guide」と「Show Invisibles」をオン。
- 「Soft Tabs」をオフ、「Tab Length」の欄に「4」を入力、「Tab Type」を「Auto」に変更。
- パッケージの設定変更は以下の画像のように、「Packages」に移動してから、インストール済みのパッケージにある「Settings」をクリック。
(1)「docblockr」の設定
- あまりこだわる必要もないが、一応「C style block comments」をオンに。
(2)「file-header」の設定
- 「Real Name」の欄に自分の名前を入れる
- 「Email Address」の欄を入力し、「Enable Filename」をオン。
- 「Date Time Format」の欄に
を入力↓
- 「Number of Empty Lines after New Header」の欄に「1」を入力
- 「Ignore List for Auto Update and Adding Header」の欄に
以下を追加で入力
↓
(3-1)「gpp-compiler」の設定(Shift-JISでC言語学習をする場合)※推奨
- 「C Compiler Options」の欄には何も入力せず、空欄のままにする。
- ソースコードに日本語を書く必要がある場合、「Core」の設定画面で「File Encoding」の欄を「Japanese (Shift JIS)」に変更。
「html」など、他の言語の編集作業をする際には、デフォルトの「Unicode (UTF-8)」に戻す。
※文字コードの切り替えは忘れやすいので、パッケージ「project-manager」を導入することで回避するのがベター。
(3-2)「gpp-compiler」の設定(UTF-8でC言語学習をする場合)※非推奨
- 「C Compiler Options」の欄に
を入力↓
C言語の学習が終わったら、「C Compiler Options」の入力欄は空欄に戻し、Cのソースコードに日本語を書くのを直ちにやめる。
(4)「linter-gcc」の設定
- 「Linter on-the-fly」をオンにする。
(5)「sublime-block-comment」の設定
- 「Pad with spaces」をオンにする
4.「Config」の設定
- まず「File」を開き、「Config...」をクリック。
- 「config.cson」というファイルが開くので、そこに以下を追加。
5.「Snippets」の設定
- 次に、「File」を開き、「Snippets」をクリック。
- 「snippets.cson」というファイルが下のように開く。
このファイルに http://lightology.hatenablog.com/entry/2017/09/07/102102 にあるテキストをコピーして、張り付ける。
まとめ
パッケージの説明が全然できず、導入した理由も書けなかった。。。
別のエントリになるだろうけど、頑張ってそのうち書きたい。
そういえば、「Snippets」に関して「autocomplete-clang」があるけど、あれはC++向けみたいだから、Cはあまり充実してない印象だった。
そもそもCの自動補完は、デフォルトで入ってるパッケージによって、次のページ:
から「Snippets」を取得してるっぽい。
でも、正直粗があるし、修正の反映も遅いみたい?だから、自作してガンガン修正したほうが手っ取り早い。
GitHubアカウントを作って、修正に協力した方が良いとも思うけど、まだシステムをよく理解してないから、おいおいという感じだな。
Visual Studio やめました
久々に Visual Studio でプログラム書こうとしたら、cのソースファイル全く開けなくなってた。
まぁ、兼ねてより Visual Studio には不満があったし、離れるにはちょうどいいのかな、と前向きに考えることにした。
1つソースファイル作るごとに、自動で関連ファイル作りすぎだしね。
ほんとぶくぶく太ってく・・・。
というわけで、重たい腰を上げて、MinGWを導入。
しかしここで、現在使用中のノートPCが64bit版であるがゆえに、パスをうまく通せない事態が発生。(これは、たまたま同じように困ってる人がいて、64bit版が原因では?と指摘しているのを見つけたので、気づくことができた)
で、MinGWの64bit版をインストールしなおし。
まあ、気づいてみればどうということはないが、それまであっちこっち調べまくる羽目に(泣)
次に、おすすめのエディタをいろいろ調べて、最終的にサクラエディタとAtomまで絞った。
サクラエディタの導入は順調にいった。
Atomの方は、動かそうとしたら、早速文字化け発生。
この文字化けに随分悩まされた。
gpp-compilerというパッケージを入れて、これで動かせれば、一番ストレスフリーにC言語の学習ができるだろうと思ったんだけど、そこからの格闘が長かった。
だって、UTF-8になおしても、文字化け治らないんだもの!(むしろ悪化)しかも、他ですでに書いてたやつはSJIS?とかいうやつで、Atomにないし、Shift-JISにしても文字化けするし!
まあ、おバカなのは自分なんだけど、とにかく発狂。
文字コードは、ホントわからんわ。
今日は1日中、cプログラミングの環境整備で時間を吸い取られた・・・。
上で述べたこと以外にも、めちゃめちゃトラブル起きてたんで、もう、へとへと・・・。
その分、勉強にはなったかな(白目)
<追記 2017/08/27>
Atom、gpp-compilerと文字化け問題は、gcc が原因だと判明。
実際、Atom関係なしに、UTF-8で書かれたものをgccでコンパイルすると同じ文字化けが起きる。
gcc "ファイル名" --input-charset=utf-8 --exec-charset=cp932
とかやれば、文字化け回避ができることも分かったけど、さて、Atomのgpp-compiler使うときはどうすれば良いのやら・・・。
まぁ、Atom上でShift-JISで書いて、Atom上でコンパイルする分には何も問題ないから、Shift-JIS安定かなぁ?
でも、起動するたびにUTF-8をShift-JISに直すのは面倒だし、将来的にhtmlとか扱うとしたらUTF-8安定みたいだから、どうしたものか・・・。