以下代码似乎可以解决问题。但是,当我真正能够看到两个不等式之间的重叠时,我想绘制方框。如何绘制如图所示的上箭头?
{\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{booktabs,array}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\draw[latex-] (-1,0) -- (9,0) ;
\draw[-latex] (-1,0) -- (9,0) ;
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$};
\draw[*-o] (2.98,0) -- (5.02,0);
\draw[very thick ] (2.98,0) -- (5.02,0);
\end{tikzpicture}
\end{document}
答案1
您可以使用相对坐标:
\draw [red, thick, -stealth] (5,0) -- ++(0,0.5) -- ++(-5,0);
这被解释为从 开始并绘制一条线到相对于当前位置(5,0)
的点。因此,一条线被绘制到。类似地,表示将线绘制到。++(0,0.5)
(5,0.5)
++(-5,0)
(5,0.5)+(-5,0)=(0,0.5)
根据@Tom Bombadil的评论,您也可以使用该|-
语法。因此
\draw [red, thick, -stealth] (5,0) |- (0,0.5);
表示从 开始(5,0)
,画一条垂直线(用 表示|
),然后画一条水平线(用 表示-
)到点(0,0.5)
。
如果你希望线条先水平然后垂直,你可以使用-|
。这是不是在这种情况下你想要什么,但可能会使这种语法更容易理解。
代码:使用相对坐标
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\draw[latex-] (-1,0) -- (9,0) ;
\draw[-latex] (-1,0) -- (9,0) ;
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$};
\draw[*-o] (2.98,0) -- (5.02,0);
\draw[very thick ] (2.98,0) -- (5.02,0);
\draw [red, thick, -stealth] (5,0) -- ++(0,0.5) -- ++(-5,0);
\draw [blue, thick, -stealth] (3,0) -- ++(0,0.4) -- ++( 4,0);
\end{tikzpicture}
\end{document}
代码:使用|-
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\draw[latex-] (-1,0) -- (9,0) ;
\draw[-latex] (-1,0) -- (9,0) ;
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$};
\draw[*-o] (2.98,0) -- (5.02,0);
\draw[very thick ] (2.98,0) -- (5.02,0);
\draw [red, thick, -stealth] (5,0) |- (0,0.5);
\draw [blue, thick, -stealth] (3,0) |- (7,0.4);
\end{tikzpicture}
\end{document}
答案2
您可以先阴影化解决方案区域,然后绘制轴和箭头
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\fill[green!20](5,0)rectangle(3,1);
\draw[latex-latex] (-1,0) -- (9,0) ;
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
\node[circle,fill,inner sep=1.5pt](a)at(5,0){};
\node[circle,draw,fill=white,inner sep=1.5pt](b)at(3,0){};
\draw[-latex,red](a)--++(0,1.5)--++(-6,0);
\draw[-latex,blue](b)--++(0,1)--++(6,0);
\end{tikzpicture}
\end{document}
答案3
intersections
这是使用库和的解决方案intersection segments
。它需要使用适当的路径进行一些设置,但结果可以非常动态和自动化。
我还用较短的命令重写了您的一些行。-{Latex}
由库提供arrows.meta
。 虽然您仍然可以使用旧的提示,但库arrows
仍受支持,尽管已被弃用。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.10}
\usetikzlibrary{arrows.meta, patterns, intersections, backgrounds}
\usepgfplotslibrary{fillbetween}
\tikzset{
ntp/.style={circle, thin, minimum size=2mm, inner sep=0, fill=white, #1}
}
\begin{document}
\begin{tikzpicture}
\draw[{Latex}-{Latex}] (-1,0) -- (9,0);
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0,1,2,3,4,5,6,7,8} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$};
\draw[name path=a, -{Latex}] (2.98,0) |-++ (5,.8);
\draw[name path=b, -{Latex}] (5.02,0) |-++ (-5,1);
\draw[very thick, name path=ax] (2.98,0) node[ntp={fill=black}] {} -- (5.02,0) node[ntp={draw}] {};
\path[name path=srt, intersection segments={of=b and a}];
\begin{scope}[on background layer]
\fill [green!30, intersection segments={of=srt and a,sequence={R1}}]
[intersection segments={of=srt and ax, sequence={--R2}}];
\end{scope}
\end{tikzpicture}
\end{document}
答案4
我建议另一种方法,不用箭头,用重叠的彩色区域代替,并使用透明度。我用的是pstricks
:
\documentclass[x11names, border=3pt]{standalone}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{pspicture*}(-1.95,-1.5)(8.95,3)
\psset{dotsize=4pt, arrowinset=0,opacity=0.5, tickstyle=bottomr}
\pnodes{A}(5,0)(5,0.5)(-2,0)
\pnodes{B}(3,0)(3,0.5)(8.95,0.5)
\psframe*[linecolor =RoyalBlue3!60](A1)(A2)
\psframe*[linecolor=Coral3!40](B0)(B2)
\psaxes[linewidth=1.2pt, yAxis = false, labelFontSize=\scriptsize]{<->}(0,0)(-2,0)(8.95,0)}
\psset{linecolor=RoyalBlue3!45!Coral3!45}
\psdot(B0)\psline(B0)(B1)
\psset{linecolor=RoyalBlue3!25!Coral3!25, linestyle=dashed, dash=2pt 1.5pt, opacity=1}
\psdot[dotstyle=o](A0)\ncline[nodesepA=2pt]{A0}{A1}
\end{pspicture*}
\end{document}