![グラフ内のノードラベルの位置を調整する](https://rvso.com/image/327769/%E3%82%B0%E3%83%A9%E3%83%95%E5%86%85%E3%81%AE%E3%83%8E%E3%83%BC%E3%83%89%E3%83%A9%E3%83%99%E3%83%AB%E3%81%AE%E4%BD%8D%E7%BD%AE%E3%82%92%E8%AA%BF%E6%95%B4%E3%81%99%E3%82%8B.png)
問題は、ノード (16, 20) の位置が線と混ざっていることです。これを解消するにはどうすればよいでしょうか。つまり、線と衝突する可能性がないように、このノードを右側にプロットしたいのです。
これが私がこれまでやってきたことです。
\documentclass[12pt, a4paper]{article}
\usepackage[a4paper,top=1 in,bottom=1 in,left=0.7 in,right=0.7 in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[misc]{ifsym}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pst-tree}
\usetikzlibrary{intersections}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[thick,latex-latex] (-1,0) -- (13,0)node[right]{$x$};
\draw[thick,latex-latex] (0,-1) -- (0,13)node[above]{$y$};
\node at (-0.3,-0.3) {};
\foreach \x/\l in {,1/2,2/4,3/6,4/8,5/10,6/12,7/14,8/16,9/18,10/20,11/22,12/24}{
\node[fill,circle,inner sep=1.5pt,label=below:$\l$] at (\x,0) {};
\node[fill,circle,inner sep=1.5pt,label=left:$\l$] at (0,\x) {};
}
\draw[thick,stealth-stealth, shorten >= -4cm, shorten <= -1cm,name path =b ](2,4) -- (6,8);
\draw[thick,stealth-stealth, shorten >= -1cm, shorten <= -1.5cm,name path =b ](10,8) -- (6,12);
\foreach \x/\y/\name/\m/\n in {4/6//8/12}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {2/4//4/8}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {6/8//12/16}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below :\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {10/8//20/16}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below left:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {8/10//16/20}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below left:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {6/12//12/24}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below left:\name($\m, \n$)}] at (\x,\y) {};
}
\draw (9,11) node[anchor=north west,rotate=0] {$x-y=-4$};
\draw (2.1,13) node[anchor=north west,rotate=0] {$2x-y-2=0$};
\end{tikzpicture}
\end{center}
\end{document}
答え1
コード内でbelow
を に変更しright
、 を 3pt に変更しました:inner sep=
\documentclass[12pt, a4paper]{article}
\usepackage[a4paper,top=1 in,bottom=1 in,left=0.7 in,right=0.7 in]{geometry}
\usepackage[utf8]{inputenc}
%\usepackage[misc]{ifsym}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pst-tree}
\usetikzlibrary{intersections}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[thick,latex-latex] (-1,0) -- (13,0)node[right]{$x$};
\draw[thick,latex-latex] (0,-1) -- (0,13)node[above]{$y$};
\node at (-0.3,-0.3) {};
\foreach \x/\l in {,1/2,2/4,3/6,4/8,5/10,6/12,7/14,8/16,9/18,10/20,11/22,12/24}{
\node[fill,circle,inner sep=1.5pt,label=below:$\l$] at (\x,0) {};
\node[fill,circle,inner sep=1.5pt,label=left:$\l$] at (0,\x) {};
}
\draw[thick,stealth-stealth, shorten >= -4cm, shorten <= -1cm,name path =b ](2,4) -- (6,8);
\draw[thick,stealth-stealth, shorten >= -1cm, shorten <= -1.5cm,name path =b ](10,8) -- (6,12);
\foreach \x/\y/\name/\m/\n in {4/6//8/12}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {2/4//4/8}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {6/8//12/16}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {10/8//20/16}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below left:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {8/10//16/20}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=3pt]right:\name($\m, \n$)}] at (\x,\y) {};
}
\foreach \x/\y/\name/\m/\n in {6/12//12/24}{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]below left:\name($\m, \n$)}] at (\x,\y) {};
}
\draw (9,11) node[anchor=north west,rotate=0] {$x-y=-4$};
\draw (2.1,13) node[anchor=north west,rotate=0] {$2x-y-2=0$};
\end{tikzpicture}
\end{center}
\end{document}
出力は次のようになります。
Torbjørn T. による追加メモ: ポイント ラベルに を使用する理由がよくわかりません\foreach
。いずれの場合も、リスト内の複数のエントリを実際にループすることはありません。ただし、位置の反復変数をもう 1 つ追加すると、1 回のループですべてのラベルを追加できます。
\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[thick,latex-latex] (-1,0) -- (13,0)node[right]{$x$};
\draw[thick,latex-latex] (0,-1) -- (0,13)node[above]{$y$};
\node at (-0.3,-0.3) {};
\foreach \x/\l in {,1/2,2/4,3/6,4/8,5/10,6/12,7/14,8/16,9/18,10/20,11/22,12/24}{
\node[fill,circle,inner sep=1.5pt,label=below:$\l$] at (\x,0) {};
\node[fill,circle,inner sep=1.5pt,label=left:$\l$] at (0,\x) {};
}
\draw[thick,stealth-stealth, shorten >= -4cm, shorten <= -1cm,name path =b ](2,4) -- (6,8);
\draw[thick,stealth-stealth, shorten >= -1cm, shorten <= -1.5cm,name path =b ](10,8) -- (6,12);
\foreach \x/\y/\name/\m/\n/\pos in
{4/6//8/12/below right,
2/4//4/8/below right,
6/8//12/16/below right,
10/8/\hspace{3pt}/20/16/below left,
8/10//16/20/right,
6/12//12/24/below left}
{
\node[fill,circle,inner sep=2.5pt,label={[inner sep=0pt]\pos:\name($\m, \n$)}] at (\x,\y) {};
}
\draw (9,11) node[anchor=north west,rotate=0] {$x-y=-4$};
\draw (2.1,13) node[anchor=north west,rotate=0] {$2x-y-2=0$};
\end{tikzpicture}
\end{center}
\end{document}