Saturday, October 24, 2015

Show Matlab figures professionally

  1. This Matlab script specifies the title Result image of the figure window, maximize the figure window, and makes the image look nice when saved to file.

    myFig = figure('Name','Result image','Units','normalized','Outerposition',[0 0 1 1],'PaperPositionMode','auto');
     
  2. This command fits the high-resolution image img on screen and suppresses Warning: Image is too big to fit on screen; displaying at 67%

    imshow(img,'InitialMagnification','fit')

    This command also suppresses the gray border, used when the figure has no title.

    imshow(img,'Border','tight','InitialMagnification','fit')
     
  3. This command shows the title Fig. 1. Distance of the figure, and allows to use symbols such as \hat{x_i}. Adjust FontSize to make complicated formulas in the title clearer.

    title('Fig. 1. Distance $d(\mathbf{x_i},\mathbf{\hat{x_i}})$','Interpreter','latex','FontSize',12.5);
     
  4. Use the following command in the command window of Matlab, to update figures without figure windows auto pop-up. Remember to change c:\yourpath to your project folder, and myscript to the name of your script (without suffix .m).

    !start /B matlab -automation -r "cd('c:\yourpath'); myscript; quit"

    There are other approaches proposed by MathWorks Support Team. However, some of them don't work on my Matlab R2014a, and docking figures makes the images too small when saved to file. Thus, I resort to start another Matlab session. Instead of use this command, you could just run another Matlab window though.
     
  5. This Matlab command saves figure to file.

    saveas(myFig, 'myImg.png')

    According to the official document, the saveas function uses a resolution of 150 DPI.

    If you'd like to change the resolution, use print. The following example sets the resolution to 200 dpi.

    print('-dpng','-r200','myImg.png')

    The following commands produce similar result. The resolution is the same as you see on the figure window.

    myFrame = getframe(myFig);

    mySnapshot = frame2im(myFrame);
    imwrite(mySnapshot, 'myImg.png')
     
  6. Download subplot_tight and suptitle from File Exchange to make subfigures look nicer.

References

Thursday, June 11, 2015

Nishishishi



Nishishishi
にしししし

Synonym Ohohoho.

Write Chinese article in LaTeX using gVim on Windows 7

  1. If you are already editing a tex file, convert it to UTF-8 encoding, then save it, with the following gVim command.

    :set fileencoding=utf8
    :w
     
  2. If you would like gVim to save your file in UTF-8 encoding by default, add the following two lines to the text file"C:\Program Files\vim\_vimrc"

    set encoding=utf-8  " The encoding displayed.
    set fileencoding=utf-8  " The encoding written to file.
     
  3. However, after I added the two lines above, the menu and text area of gVim became gibberish. Thus, I add the following lines to "C:\Program Files\vim\_vimrc" to set the menu and text area to English.

    set langmenu=en_US
    let $LANG = 'en_US'
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vi
     
  4. If you use VIM-Latex to compile your tex file, you have to make VIM-Latex to compile with XeLaTeX rather than pdfLaTeX. Add the following line to "C:\Program Files\vim\_vimrc"

    let g:Tex_CompileRule_pdf='xelatex --interaction=nonstopmode $*'
     
  5. Add the following lines to the preamble of your tex file, after \documentclass.

    \usepackage{fontspec} %加這個就可以設定字體
    \usepackage{xeCJK} % 讓中英文字體分開設置
    \setCJKmainfont{標楷體} %設定中文為系統上的字型,而英文不去更動,使用原TeX字型
    \XeTeXlinebreaklocale "zh"
    \XeTeXlinebreakskip = 0pt plus 1pt %這兩行一定要加,中文才能自動換行
     
  6. However, on my machine, XeLaTeX gave me the following error message.

    fontspec error: "font-not-found" ! ! The font "????" cannot be found.

    Thus, I changed this line above

    \setCJKmainfont{標楷體}

    to

    \setCJKmainfont{DFKai-SB}

    It won't let me use Chinese font names, strange!

  7. If you want to use 新細明體, try the following line instead.

    \setCJKmainfont{PMingLiU}

    If you want to use 微軟正黑體, try the following line instead.

    \setCJKmainfont{Microsoft JhengHei}

Reference

Thanks to all the answerers on StackExchange.
LaTeX tutorial by Academia Sinica (in Chinese)