Aligntaでvimで行末のコメントを揃える
vimで行末のコメントを揃えるときの話。 矩型選択などを利用して揃えるものはあるが、プラグインを使った解説をしているものがなかったのでメモ。
準備
Alignta
を入れる。
Alingta
は、文章整形用のvim plugin。
同様のツールにAlign
があるが、Alignta
の方が高機能?
markdownのテーブルの整形などにも使えるので、入れておいて損はない。
インストール
NeoBundleの場合は下記でOK。
NeoBundle 'h1mesuke/vim-alignta'
やり方
下記のコードを例にする。
int a; // hoge const int b = 1; // fuga const double c = 2.0; // hage
揃えたい行を全て選択する。
:
を押して、下記コマンドを入力する。
:'<,'>Alignta <- //
下記のように揃う。
int a; // hoge const int b = 1; // fuga const double c = 2.0; // hage
コマンドの意味
:'<,'>Alignta <- //
'<,'>
は選択範囲を表すvimのコマンド。Alignta
はAligntaのコマンド。<-
はAligntaの整形用のオプションで、揃える文字(この場合//
)の左側(ソースコード部分)は変更しないということ。//
は揃える文字の先頭。
補足
行末コメントを揃えるのはメンテナンス性が悪いので、googleのcoding guidelineなどでは使用しないように記載されている場合もあるが、Cの場合はマクロを見やすくするために¥
を行末にいれることがしばしばある。
pythonのpackagesのimportまとめ
pythonのpackagesのimportの仕方と使いかたのメモ。
pythonはディレクトリ内に__init__.py
を置いておくと、ディレクトリはpackagesとして認識される。
__init__.py
はpackagesの初期化処理を記述し、なければ空で良い。
まとめ
- packages内のフォルダ名及びファイル名はC++などでいうところの名前空間のような役割。
- 単純に
import
する場合は、その名前空間を利用可能にするだけで、省略はできない - packages名などの省略をしたい場合は
from packages import sub
などを利用する。
テストファイル
ディレクトリ構成は以下。
├── main.py └── packages ├── __init__.py ├── egg.py └── sub ├── __init__.py └── sub_egg.py
main.py
import sys #to get function name. def test0(): print sys._getframe().f_code.co_name print " ", #packages.egg.egg() error def test1(): print sys._getframe().f_code.co_name print " ", import packages.egg packages.egg.egg() def test2(): print sys._getframe().f_code.co_name print " ", from packages import egg egg.egg() #packages.egg.egg() error def test3(): print sys._getframe().f_code.co_name print " ", import packages from packages import egg packages.egg.egg() def test4(): print sys._getframe().f_code.co_name print " ", import packages.sub.sub_egg packages.sub.sub_egg.sub_egg() #sub_egg() error def test5(): print sys._getframe().f_code.co_name print " ", from packages.sub import sub_egg sub_egg.sub_egg() def test6(): print sys._getframe().f_code.co_name print " ", from packages.sub.sub_egg import sub_egg sub_egg() def test7(): print sys._getframe().f_code.co_name print " ", import packages packages.sub.sub_egg.sub_egg() if __name__ == '__main__': test0() test1() test2() test3() test4() test5() test6() test7()
packages/egg.py
def egg(): print "packages.egg"
packages/sub/sub_egg.py
def sub_egg(): print "packages.sub.sub_egg"
実行結果
packages.egg packages.egg packages.egg packages.sub.sub_egg packages.sub.sub_egg packages.sub.sub_egg packages.sub.sub_egg
ublasのouter_prodの計算式
boostのublasにはouter_prodというベクトルとベクトルの転置の積を計算する関数が存在する。 どちらのベクトルが転置されているかちゃんと書いたものがなかったのでメモ。 直積の演算としては直感通りの式。 $$ x = \left( \begin{array}{c} x_{1} \\ x_{2} \\ \end{array} \right), y = \left( \begin{array}{c} y_{1} \\ y_{2} \\ \end{array} \right) $$ $$ \mathrm{outer\_prod}(x, y) = xy^{T} = \left( \begin{array}{cc} x_{1}y_{1} & x_{1}y_{2} \\ x_{2}y_{1} & x_{2}y_{2} \\ \end{array} \right) $$
#include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/matrix.hpp> namespace ublas = boost::numeric::ublas; int main(int argc, char const* argv[]) { ublas::vector<double> x(2); x(0) = 1.0; x(1) = 2.0; ublas::vector<double> y(2); y(0) = 3.0; y(1) = 4.0; ublas::matrix<double> A = ublas::outer_prod(x, y); std::cout << A(0, 0) << ", " << A(0, 1) << std::endl;//3, 4 std::cout << A(1, 0) << ", " << A(1, 1) << std::endl;//6, 8 return 0; }
数式が使える最強のプレゼンテーション環境
うれしい
使うもの
サンプル
導入手順
- ローカルに適当にディレクトリ(
slides
とする)を作る。 slides
ディレクトリにindex.html
を作り、同じディレクトリにcontents.md
を配置する。index.html
をブラウザで開けばOKスライドが見れる。index.html
<DOCTYPE html> <html> <head><title>Presentation</title></head> <body> <script src="https://remarkjs.com/downloads/remark-latest.min.js" type="text/javascript"></script> <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript"></script> </script> <script type="text/javascript"> var slideshow = remark.create({ sourceUrl: "contents.md" }); // Setup MathJax MathJax.Hub.Config({ tex2jax: { skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'] } }); MathJax.Hub.Queue(function() { $(MathJax.Hub.getAllJax()).map(function(index, elem) { return(elem.SourceElement()); }).parent().addClass('has-jax'); }); MathJax.Hub.Configured(); </script> </body> </html>
contents.md
# slide 1 - 1ページ目 --- # slide 2 - 2ページ目 $$ x = a_{1} + b^{2} $$ ## フーリエ変換 $$ F(u) = \int\_{-\infty}^{\infty} f(x)\mathrm{e}^{-j2\pi ux}dx $$ ## 2次元のフーリエ変換 $$ F(u,v) = \int\_{-\infty}^{\infty} \int\_{-\infty}^{\infty} f(x,y)\mathrm{e}^{-j2\pi (ux + vy )}dxdy $$
ここまでで、ローカルでスライドが見れる環境ができる。 次は、github pagesを作って、github上でスライドが見れるようにする。
4. slides
上でgitレポジトリを作り、全部コミット。
git init git add . git commit -m 'Initial commit.'
5. githubにレポジトリを作り、slides
のレポジトリをpushする。
git remote add https://github.com/username/repository_namae/ git push origin master
6. gh-pagesを使う為、gh-pages
ブランチを作って、githubにpushする。
git checkout -b gh-pages git push origin gh-pages
7. https://username.github.io/repository_name
にアクセスするとレポジトリトップのindex.html
のファイルが表示される。
- uploadしたデータが反映されるまで10分ほどかかるみたいなので気長にまつ。
username
はgithubのユーザ名reposiotry_name
はレポジトリの名前
キー操作
j
次のスライドk
前のスライドc
スライドのclone- スライドを clone すると、元のスライドでページ移動した際、clone 先のスライドも同期して移動する。
f
フルスクリーンp
プレゼンモードh
ヘルプの表示
補足
contents.md
では、普通にmarkdown中にmathjaxを書けばOK。 具体的には、$$
で数式を囲む。- mathjaxのURLに
delayStartupUntil=configured
オプションを含めると、markdownで問題になる_
のエスケープなどが不要になるので便利。
個人的には、スライドごとにレポジトリを作る気分にあんまりならないので、下記のように1つのレポジトリをまとめている。
特に工夫はないが、レポジトリのトップのcontents.md
はレポジトリ内のスライドへのリンクテーブルになっている個人的に便利。
# link * [directory_name](http://user_name.github.io/repository_name/directory_name/) * sample
3. レポジトリにスライドごとにディレクトリを作り、各ディレクトリにindex.html
とcontents.md
を作る。
参考
NeoBundleCleanが使えない
NeoBundleCleanはdangerousなので削除したとのこと。
How to Uninstall Packages? · Issue #356 · Shougo/neobundle.vim · GitHub
プラグインを読み込まないようにしたい場合は、NeoBundle hoge/fuga.vim
の記述を消せば読み込まれないとのこと。
プラグインを削除したい場合は、
call neobundle#begin(expand('~/.vim/bundle/'))
で指定しているディレクトリからpluginのディレクトリを手動で削除する。
C++で配列の型のtypedef
配列のtypedefをするときは次のようにする。
//int[2]のtypedef typedef int type[2];
windowsでgithubのrepositoryにpushするときにエラーがでる場合の対応
使用しているgitは以下。
$ git version git version 2.6.3.windows.1
次のコマンドを実行すると、
git remote add origin https://hogehoge.gihttps://github.com/user_id/hogehoge.git git push origin master
下記のエラーがでる。
!!!bash: /dev/tty: No such device or address!!! !!!error: failed to execute prompt script (exit code 1)!!! !!!fatal: could not read Username for 'https://github.com': Invalid argument!!! !!!vimshell: exit 128 "git push origin master"!
上記はvimshellでのエラーだが、cmdから直接実行しても同様のエラーがでる。
対処法?
1. 以下の方法によるが、試してない。 [Windows] bash: /dev/tty: No such a device or address · Issue #8984 · atom/atom · GitHub
以下のURLをremote repositoryのURLとして設定する。
https://<username>:<password>@github.com/<username>/<repo_name>.git
たとえば、次の場合、 * username:hoge * password:fuga * repo_name:hage
git remote add origin https://hoge:fuga@github.com/hage/hage.git
2. こちらは、cmd上で動作は確認。vimshell上では未確認。
https://hoge@github.com/hage/hage.git
とするとパスワードが求められるので、入力する。