Eli의 여백

바쁜 나날들 사이에서 생각났던 이런저런 것들을 적어봅니다.

자료수집

정확한 좌표로 그림 그리기 (metapost와 manim)

Eli♪ 2021. 3. 13. 21:31

 

그림 출처: http://kahrstrom.com/mathematics/illustrations.php

MetaPost 관련

수학이나 물리학 논문들을 보면 위와 같이 흑백으로 그렸지만 멋이 철철 넘치는 그림들이 있다. 나도 그려보고 싶어서 이리저리 찾기 시작했다. 원했던 것은 다음과 같다.

 

1. 결과물이 멋있을 것

2. 좌표를 정확히 지정 가능할 것

3. 벡터 이미지로 저장이 가능할 것

 

이 3가지를 모두 만족하는 것으로 metapost라는게 있었다. 몇십 년의 역사를 자랑하며, TeX안에 패키지로 포함되는 것 같다. TikZ라는것도 있다는 것 같은데, TeX는 익숙치가 않아서...

 

하여간 내용은 방대한데 정리할 시간이 없어서 대충 링크들을 남긴다.

 

Metapost라는게 있다는 글. 나는 LaTeX 안쓸거라 standalone으로 깔고싶었는데 무조건 TeX 패키지에 포함이다. ㅠㅠ

https://physics.stackexchange.com/a/2213

 

What software programs are used to draw physics diagrams, and what are their relative merits?

Undoubtedly, people use a variety of programs to draw diagrams for physics, but I am not familiar with many of them. I usually hand-draw things in GIMP which is powerful in some regards, but it is...

physics.stackexchange.com

생물학 논문에는 biorender를 많이 쓴다는 글. 나도 박사학위 프로포절때 biorender를 써서 그림을 그린 슬라이드가 있다. 근데 이건 생물이나 의학쪽에 관한거지 물리쪽 그림처럼 고전적인 위엄이 느껴지지는 않는다.

www.nature.com/articles/d41586-020-01404-7

이 글 처음에 넣었던 그림을 그린 사람. metapost로 주로 그리고, 가끔 postscript를 직접(!) 코딩해서 그림을 그리는 것 같다.

kahrstrom.com/mathematics/illustrations.php

 

Johan Kåhrström - Mathematics

Illustrations I very much enjoy making mathematical illustrations. Here are some examples. Created in MetaPost unless otherwise stated. Click image for PDF version. Trigonometric funtions. A simple illustration showing the definition of sine and cosine. Di

kahrstrom.com

metapost 튜토리얼. 코드와 미리보기를 같이 제공한다.

www.ursoswald.ch/metapost/tutorial.html

 

MetaPost

Paths.mp MetaPost paths are objects, as are numerics or strings. MetaPost objects can be combined to form arrays. MetaPost arrays can be addressed like, e.g., Java arrays. In MetaPost, however, array indexes are not restricted to nonnegative integers (i. e

www.ursoswald.ch

metapost 위키 페이지 (예시도 있음)

en.wikipedia.org/wiki/MetaPost

 

MetaPost

Programming language MetaPost refers to both a programming language and the interpreter of the MetaPost programming language. Both are derived from Donald Knuth's Metafont language and interpreter. MetaPost produces vector graphic diagrams from a geometric

en.wikipedia.org

metapost 관련 링크들

tug.org/metapost.html

 

MetaPost - TeX Users Group

MetaPost on the Web This page has a list of links related to MetaPost, a powerful tool for creating graphics in scalable PostScript. It was written by John Hobby, based on Metafont by Donald Knuth. Around 2007, it was separated into a frontend and backend

tug.org

metapost에서 그린 것을 svg로 출력하는 방법

tug.org/metapost/runningmp.html

 

Running MetaPost - TeX Users Group

Running MetaPost This page just gives the most basic introduction to getting output from MetaPost. For tutorials, references, and much more, see the MetaPost home page. Contents: Running standalone - SVG output; filename extensions - Running inside ConTeXt

tug.org

metapost example 모음. 다양한 결과물들을 한눈에 훑어볼 수 있다.

tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html

 

Metapost : exemples

 

tex.loria.fr

TeXLive 설치 (metapost는 texlive같은거 안에 포함되어있는 형태로만 받을 수 있다. 참고로 풀설치하면 시간이 1시간 반 정도 걸린다.)

woogyun.tistory.com/663

 

윈도 한글 LaTeX 설치(TeXLive 2020)

지난 글에서 윈도 한글 LaTeX 설치 방법을 TexLive 2016 버전 기준으로 설명했었다(woogyun.tistory.com/533). 이 방법대로 한동안 잘 설치하여 사용하고 있었는데, 버전이 높아짐에 따라 화면도 많이 바뀌어

woogyun.tistory.com

TeXstudio 설치 페이지. 다른 프로그래밍 언어들의 IDE에 해당한다고 보면 될 것 같다. TeXLive를 설치하였기에 깔진 않았다.

texstudio.org/

 

TeXstudio - A LaTeX editor

Welcome to TeXstudio TeXstudio is an integrated writing environment for creating LaTeX documents. Our goal is to make writing LaTeX as easy and comfortable as possible. Therefore TeXstudio has numerous features like syntax-highlighting, integrated viewer,

texstudio.org

metapost에서 label에 variable 사용. 예시가 변수를 $같은걸 써놔서 헷갈리게 되어있다. 

tex.stackexchange.com/questions/34455/metapost-label-based-on-variable-value

 

Metapost label based on variable value

I want to label a scale based on a variable value and I wrote a metapost code like this. $ := 1; for i = -.3cm step .6cm until 3.6cm: label.bot(str$,(i,-3.7cm)); $ := incr($); endfor; It ...

tex.stackexchange.com

metapost 코드를 인터넷상에서 돌려서 프리뷰 svg 이미지를 볼 수 있게 해주는 툴

www.tlhiv.org/mppreview/

 

MetaPost Previewer by Troy Henderson

 

www.tlhiv.org

맨 처음 올렸던 이미지에서 외곽선 R을 구현하려고 했는데 넣을 수 없어서 찾았던 자료

tex.stackexchange.com/questions/556790/mathbb-in-txfonts

 

Mathbb in txfonts

I used txfonts but I don't like it when I use it with mathbb. I like it more when I use Computer Modern instead. And I want to change it to something like Computer Modern. How can I do it?

tex.stackexchange.com

그런데 나머지 글자들이 times new roman이 아니었고, 이럭저럭 하다가 최종적으로는 이렇게 구현했다.

TEXPRE("%&latex" & char(10) & "\documentclass{article}\usepackage{amsmath}\usepackage{newtxmath,newtxtext}\DeclareMathAlphabet{\mathbb}{U}{msb}{m}{n}\begin{document}");
input TEX
label(TEX("\( \mathbb{R}^m  \) ") scaled defaultscale,1.2(0,xmax) scaled scalefactor);

관련페이지:

tex.stackexchange.com/questions/317554/change-font-to-times-new-roman-in-pdflatex

 

Change font to "Times New Roman" in PDFLaTeX

Can anyone help me to change the font to "Times New Roman" in PDFLaTeX? I already tried using the commands below without results: % Font selection \usepackage[T1]{fontenc} \usepackage{times} \

tex.stackexchange.com

metapost와 유사한 그림그리는 툴인 TikZ와 PGF 결과물 갤러리

texample.net/tikz/examples/all/?page=1

 

TikZ and PGF examples

 

texample.net

PGF github page

github.com/pgf-tikz/pgf

 

pgf-tikz/pgf

A Portable Graphic Format for TeX. Contribute to pgf-tikz/pgf development by creating an account on GitHub.

github.com

다양한 그림그리는 툴 예시. matplotlib은 matlab만도 못한 퀄이 나오며, 여기서manim이란걸 처음으로 발견했다.

math.stackexchange.com/a/3938216

 

Software for drawing geometry diagrams

What software do you use to accurately draw geometry diagrams?

math.stackexchange.com

Metapost 연습해본 것들

아래 코드로 생성한 fig 5, 6, 7

prologues:=3;
outputtemplate :="%j-%c.svg";
outputformat:="svg";
defaultfont:="ptmr8r";
defaultscale := 12pt/fontsize defaultfont;


beginfig (1);
    pair A, B, C;
    A:=(0,0); B:=(1cm,0); C:=(0,1cm);
    draw A..B..C--cycle;
endfig;

beginfig (2);
    z0=(0,0);
    z1=(60,40);
    z2 =(40,90);
    z3 =(10,70);
    z4 =(30,50);
    draw z0..z1..z2..z3--z4--cycle;
endfig;

beginfig (3);
    z0=(0,0);
    z1=(60,40);
    z2 =(40,90);
    z3 =(10,70);
    z4 =(30,50);
    draw z0{down}..z1{up}..z2{left}..z3..z4;
endfig;

beginfig(4);
for k=0 upto 9:
    draw (0,0){dir 45}..{dir 10k}(6cm,0);
endfor
endfig;

beginfig(5);

z1=-z2=(2cm,0);
x3=-x6=3cm;
x3+y3=x6+y6=11cm;
z4=1/3[z3,z6];
z5=1/3[z6,z3];
z20=whatever[z1,z3]=whatever[z2,z4];
z30=whatever[z1,z4]=whatever[z2,z5];
z40=whatever[z1,z5]=whatever[z2,z6];
draw z1--z20--z2--z30--z1--z40--z2;
pickup pencircle scaled 2pt;
draw z1--z2;
draw z3--z6;
show z20;
dotlabels.rt(1,2,3,4,5,6,20,30,40);
label.lrt(btex $\displaystyle y={2\over1+\cos x}$ etex scaled defaultscale, 1/2[z1,z20]);
endfig;

beginfig (6);
uz = 3cm;
path p,q,r,s;
p = (-uz,0)..(0,-uz)..(uz,0);
q = p{up}..(0,0){dir -120}..{up}cycle;
r = fullcircle scaled 0.5uz;
s = buildcycle(r,q);
fill s withcolor red; % no overlap with bottom half
%fill q withcolor blue;
draw r;
draw q;
%fill s;
dotlabel.rt("p1", q intersectionpoint r);
show length r;
endfig;


input tex;
beginfig (7);
numeric a, #, t[];
3.2a = 5cm;
path fun;
# = 0.5; % don't know what this is;
fun = ( (0,-1#)..(1,.5#){right}..(1.9,0.2#){right}..{curl 0.1}(3.2,2#)) yscaled(1/#) scaled a;
x1 = 2.5a;
n:=5;
for i=1 upto n:
    (t[i],whatever) = fun intersectiontimes ((x[i], -infinity)--(x[i],infinity));
    z[i] = point t[i] of fun;
    z[i]-(x[i+1],0) = whatever*direction t[i] of fun;
    % draw (x[i],0)--z[i]--(x[i+1],0);
    draw (x[i],0)--z[i] dashed evenly;
    draw z[i]--(x[i+1],0);
    fill fullcircle scaled 3bp shifted z[i]; % make dots
    % label.ulft( TEX( "$f_{"&decimal(i)&"}$" ) scaled defaultscale, z[i]);
    label.ulft( TEX( "$f_{"&decimal(i)&"}$" ), z[i]);
endfor
for i=1 upto (n+1): % https://stackoverflow.com/questions/15930810/metapost-using-loop-variables-in-labels
    label.bot( TEX( "$x_{"&decimal(i)&"}$" ), (x[i],0));
endfor
draw (0,0)--(3.2a,0);
pickup pencircle scaled 1pt;
draw fun;
endfig;

end.

 

prologues:=3;
outputtemplate :="%j-%c.svg";
outputformat:="svg";

% https://tex.stackexchange.com/questions/317554/change-font-to-times-new-roman-in-pdflatex
input TEX;
TEXPRE("%&latex" & char(10) & "\documentclass{article}\usepackage{amsmath}\usepackage{newtxmath,newtxtext}\DeclareMathAlphabet{\mathbb}{U}{msb}{m}{n}\begin{document}");
TEXPOST("\end{document}");


beginfig (1);
defaultfont:="ptmr8r";

%defaultfont := "ptmbi8r";
defaultscale := 24pt/fontsize defaultfont;
scalefactor := 1cm;


path boundaries;
path v[], h[], maskpath;
xmax:=6;
boundaries = (xmax,xmax)--(-xmax,xmax)--(-xmax,-xmax)--(xmax,-xmax)--cycle;
for k = -xmax upto xmax:
    v[k] = (k,-xmax)--(k,xmax);
    h[k] = (-xmax,k)--(xmax,k);
    if k = 0:
        pickup pencircle scaled 1pt;
    else:
        pickup pencircle scaled 0.5pt;
    fi;
    draw v[k] scaled scalefactor;
    draw h[k] scaled scalefactor;
endfor

pickup pencircle scaled 2pt; % https://tex.stackexchange.com/questions/8260/what-are-the-various-units-ex-em-in-pt-bp-dd-pc-expressed-in-mm
draw boundaries scaled scalefactor;

pickup pencircle scaled 1pt;
z1= (xmax,0); z2= (0,0.85*xmax); z3 = (-xmax*0.5,xmax*2/3); z4 = (-xmax,0);
maskpath = ( 0.9z1{up}..tension 1.7..z2..z3..tension 1.2..{down}4/5z4 ) scaled scalefactor;
draw maskpath dashed evenly;
draw (maskpath yscaled -1) dashed evenly;

% ======= Lables =====
label.llft(TEX("\( x_{" & decimal(3) & "} \)") scaled defaultscale,(-xmax,xmax) scaled scalefactor);
label.lft(TEX("\( x(U) \)") scaled defaultscale, 1/3[z1,-z2] scaled scalefactor);
label(TEX("\( \mathbb{R}^m  \) ") scaled defaultscale,1.2(0,xmax) scaled scalefactor);
endfig;


beginfig(100)
%% Definitions
path boundaries;
path v[], h[], maskpath;
xmax:=3cm;
z0 = (0,0); z1 = (xmax,xmax); 
z2 = z1 xscaled -1; z3 = z1 rotatedaround(z0,180); z4=z1 yscaled -1;

%% main calc
boundaries = z1--z2--z3--z4--cycle;

%% Draw
draw boundaries yscaled 0.1;

% labels
last := 10;
for i := 0 upto last:
label(TEX("\( x_{" & decimal(i) & "} \)"), (5mm*(i-last/2),0));
endfor
endfig;

end.

Manim 관련

manim 커뮤니티 버전. 2020년부터 시작했다고 한다. 원래는 3b1b가 만든게 맞고, 그걸 개선시키기 위한 프로젝트이다.

github.com/ManimCommunity/manim/

 

ManimCommunity/manim

Animation engine for explanatory math videos. Contribute to ManimCommunity/manim development by creating an account on GitHub.

github.com

 

manim documentation. 기본 예시를 볼 수 있다.

docs.manim.community/en/latest/tutorials/quickstart.html

 

Quickstart

This document will lead you step by step through the necessary procedure to get started with manim for the first time as soon as possible. This tutorial assumes you have already installed manim fol...

docs.manim.community

내가 원했던 것처럼 그림을 그리는 것에 대한 책. raw postscript에서 그림 어떻게 그리는지에 대해 써있는데 따라하려니까 너무 오래걸릴것같아서 일단 킵.

www.math.ubc.ca/~cass/graphics/manual/

 

Mathematical Illustrations

This manual has been available on this site since about 1996, with improvements taking place frequently. The current version has been published as a book of about 350 pages by Cambridge University Press. By agreement with the Press, however, it will remain

www.math.ubc.ca

이 그림을 그려보려고 했는데 transfomation이 간단하진 않아서 일단 여기서 멈춤.

 

위 그림 관련해서 찾아본 matlab에서의 transformation 함수

kr.mathworks.com/matlabcentral/answers/472396-simulate-perspective-transformation-depending-on-a-satellite-position

 

Simulate perspective transformation depending on a satellite position - MATLAB Answers - MATLAB Central

 

kr.mathworks.com

맨 처음 그림과 위의 책 그림을 위한 perspective transform에 관한 간단한 설명.

boysboy3.tistory.com/64

 

21. Perspective Transformation(투영변환)

Perspective Transformation == Homography Matrix == Projective Transformation (세 용어다 각각 차이점은 존재하지만 개념상 같은 개념이라고 생각을 해도 된다.) Homograhpy 한 평면을 다른 평면에 투영(Proje..

boysboy3.tistory.com

 

3b1b가 직접 작성한 설치 가이드. 그런데 커뮤니티버전이 앞으로 계속 더 업글될것이므로 deprecated될 가능성 높음

3b1b.github.io/manim/getting_started/installation.html

 

Installation - manim documentation

Previous Home

3b1b.github.io

3b1b 버전의 manim을 설치하는 방법. 이걸 참고해서 커뮤니티 버전으로 따라해봤으나 sox는 설치가 불가능했고, pip를 쓰기 때문에 여러모로 불안한 방법. 어쨌거나 ffmpeg 패키지만 있어도 quickstart 예제는 구현이 가능했다.

talkingphysics.wordpress.com/2020/07/06/clean-install-of-manim/

 

Clean Install of manim

I got a new Mac from work and I didn’t have manim installed. However, when I followed my own instructions I ran into issues. I don’t know if others will have similar problems but I thou…

talkingphysics.wordpress.com

 

---- 2021-12-16 추가 ----

후속 글이 완성되었다. 궁금한 사람은 참고하기 바란다.

https://projecteli.tistory.com/197

 

논문용 멋진 그림 그리는 법에 관한 자료

이전 글 1: 논문용 그림 그리는 기준 및 pdf export에 관한 생각 이전 글 2: 정확한 좌표로 그림 그리기 (metapost와 manim) 이 글은 위의 두 가지 글에서 이어지는 내용이라고 보면 된다. 작성계기 사실

projecteli.tistory.com

---------------------------