Kodama's home / tips.

LaTeX でグラフの座標軸を書く

使用例:

\axis{10}{20}{30}{40}
\axisDiv{10}{20}{30}{40}{5}{1}{5}{1}
axis は縱-横の座標軸を指定した長さ(mm)で書く. axisDiv は目盛りつき.

試験問題の作成等で安直にグラフの軸を書くマクロがほしくなってでっちあげた. 完全なグラフを描くところまではやらない. まあ, 試験問題作成用だからねぇ.

以下のコ−ドを LaTeX のソ−ス中に組み込んでつかう. 軸の長さの計算等は 長さとして扱う方がすっきりするが, カウンタ−として計算した.

\def\axis#1#2#3#4{{%
% Draw axis for a graph.
% axis. 2001-02-24 K.Kodama
% #1: width of negative part. #2: width of positive part.
% #3: height of negative part. #4: height of positive part.
\unitlength=1mm
\count1=#1\advance\count1 by#2 \count2=#3\advance\count2 by#4
\begin{picture}(\the\count1,\the\count2)
\put(0,#3){\line(1,0){#1}}\put(#1,#3){\vector(1,0){#2}}
\put(#1,0){\line(0,1){#3}}\put(#1,#3){\vector(0,1){#4}}
\end{picture}
}}


\def\axisC#1#2#3#4{{%
% Draw axis for a graph.
% axis. 2001-02-24 K.Kodama
% #1: width of negative part. #2: width of positive part.
% #3: height of negative part. #4: height of positive part.
\unitlength=1mm
\count1=#1\advance\count1 by#2 \count2=#3\advance\count2 by#4
\begin{picture}(\the\count1,\the\count2)
\put(0,#3){\line(1,0){#1}}\put(#1,#3){\vector(1,0){#2}}
\put(#1,0){\line(0,1){#3}}\put(#1,#3){\vector(0,1){#4}}
% caption
\setbox0=\hbox{$0$}\setlength{\dimen1}{\ht0}
\put(#1,#3){\kern-\wd0\raisebox{-1.2\dimen1}{\box0}}
\setbox0=\hbox{$x$}\setlength{\dimen1}{\ht0}
\put(\the\count1,#3){\kern-\wd0\raisebox{-1.5\dimen1}{\box0}}
\setbox0=\hbox{$y$}\setlength{\dimen1}{\ht0}
\put(#1,\the\count2){\kern-1.5\wd0\raisebox{-\dimen1}{\box0}}
\end{picture}
}}


\def\axisDiv#1#2#3#4#5#6#7#8{{
% axisDiv. 2001-02-24 K.Kodama
% #1: width of negative part. #2: width of positive part.
% #3: height of negative part. #4: height of positive part.
% #5: interval of horizontal division, #6: unit of horizontal div.
% #7: interval of vertical division, #8: unit of vertical div.
\unitlength=1mm
\count1=#1\advance\count1 by#2 \count2=#3\advance\count2 by#4
\begin{picture}(\the\count1,\the\count2)
\put(0,#3){\line(1,0){#1}}\put(#1,#3){\vector(1,0){#2}}
\put(#1,0){\line(0,1){#3}}\put(#1,#3){\vector(0,1){#4}}
% horizontal axis
\count1=#1\advance\count1 by#5 \count2=#3\advance\count2 by -2
\setbox0=\hbox{#6}\setlength{\dimen1}{\ht0}
\put(\the\count1,\the\count2){\kern-0.5\wd0\raisebox{-\dimen1}{\box0}}
\count1=0\count2=0
\loop\advance\count1 by#5\advance\count2 by1\ifnum\the\count1<#1\repeat
\multiput(#1,#3)(-#5,0){\the\count2}{\line(0,-1){1}}
\count1=2\count2=0
\loop\advance\count1 by#5\advance\count2 by1\ifnum\the\count1<#2\repeat
\multiput(#1,#3)(#5,0){\the\count2}{\line(0,-1){1}}
% vertical axis
\count1=#1\advance\count1 by-1 \count2=#3\advance\count2 by#7
\setbox0=\hbox{#8}\setlength{\dimen1}{\ht0}
\put(\the\count1,\the\count2){\kern-\wd0\raisebox{-0.5\dimen1}{\box0}}
\count1=0\count2=0
\loop\advance\count1 by#7\advance\count2 by1\ifnum\count1<#3\repeat
\multiput(#1,#3)(0,-#7){\the\count2}{\line(-1,0){1}}
\count1=2\count2=0
\loop\advance\count1 by#7\advance\count2 by1\ifnum\count1<#4\repeat
\multiput(#1,#3)(0,#7){\the\count2}{\line(-1,0){1}}
\end{picture}
}}

\def\axisDivC#1#2#3#4#5#6#7#8{{
% axisDivC. 2001-02-24 K.Kodama
% #1: width of negative part. #2: width of positive part.
% #3: height of negative part. #4: height of positive part.
% #5: interval of horizontal division, #6: unit of horizontal div.
% #7: interval of vertical division, #8: unit of vertical div.
\unitlength=1mm
\count1=#1\advance\count1 by#2 \count2=#3\advance\count2 by#4
\begin{picture}(\the\count1,\the\count2)
\put(0,#3){\line(1,0){#1}}\put(#1,#3){\vector(1,0){#2}}
\put(#1,0){\line(0,1){#3}}\put(#1,#3){\vector(0,1){#4}}
% caption
\setbox0=\hbox{$0$}\setlength{\dimen1}{\ht0}
\put(#1,#3){\kern-\wd0\raisebox{-1.2\dimen1}{\box0}}
\setbox0=\hbox{$x$}\setlength{\dimen1}{\ht0}
\put(\the\count1,#3){\kern-\wd0\raisebox{-1.5\dimen1}{\box0}}
\setbox0=\hbox{$y$}\setlength{\dimen1}{\ht0}
\put(#1,\the\count2){\kern-1.5\wd0\raisebox{-\dimen1}{\box0}}
% horizontal axis
\count1=#1\advance\count1 by#5 \count2=#3\advance\count2 by -2
\setbox0=\hbox{#6}
\setlength{\dimen1}{\ht0}
\put(\the\count1,\the\count2){\kern-0.5\wd0\raisebox{-\dimen1}{\box0}}
\count1=0\count2=0
\loop\advance\count1 by#5\advance\count2 by1\ifnum\the\count1<#1\repeat
\multiput(#1,#3)(-#5,0){\the\count2}{\line(0,-1){1}}
\count1=2\count2=0
\loop\advance\count1 by#5\advance\count2 by1\ifnum\the\count1<#2\repeat
\multiput(#1,#3)(#5,0){\the\count2}{\line(0,-1){1}}
% vertical axis
\count1=#1\advance\count1 by-1 \count2=#3\advance\count2 by#7
\setbox0=\hbox{#8}
\setlength{\dimen1}{\ht0}
\put(\the\count1,\the\count2){\kern-\wd0\raisebox{-0.5\dimen1}{\box0}}
\count1=0\count2=0
\loop\advance\count1 by#7\advance\count2 by1\ifnum\count1<#3\repeat
\multiput(#1,#3)(0,-#7){\the\count2}{\line(-1,0){1}}
\count1=2\count2=0
\loop\advance\count1 by#7\advance\count2 by1\ifnum\count1<#4\repeat
\multiput(#1,#3)(0,#7){\the\count2}{\line(-1,0){1}}
\end{picture}
}}

Kodama's home / tips.