2

I'm trying to replicate the following figure.

enter image description here

This is my attempt, but I want to get something more precise.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc} 

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\draw[extended line,thick,<->] (0,0) -- (4.5,0) node[anchor=north west] {\small state 1};
\draw[extended line,thick,<->] (0,0) -- (0,4.5) node[anchor=south east] {\small state 2};

\coordinate (A) at (0,0);
\coordinate (B) at (4,-1);

\draw [extended line=2cm, <->] (A) -- (B) coordinate[midway] (M)
node[pos=1.2,right=1em, font=\small]{$X$};     


\draw [dashed, <->] ($(M)!3cm!270:(A)$) -- ($(M)!3cm!90:(A)$) node[pos=0.9,right=1em, font= \small]{$m=x^*+\varepsilon$ space of discount factors} coordinate[pos=0.2] (x);

\node [dot=right:$x^{*}$] at (M) {}; 
\fill [red] (M) circle [radius=2pt] ;


\coordinate (C) at (2,-1);
\coordinate (D) at (1,4);
\coordinate (P) at (3,0);


\draw [shorten >=-30pt,shorten <=-30pt] (C) -- (D) node [pos=1.2,right=1em, font=\small]{$p=1$};
\draw [shorten >=-30pt,shorten <=-30pt] (P) -- +($(D)-(C)$) node [pos=1.2,right=1em, font=\small]{$p=2$};

\draw[blue,-latex] (0,0) -- (x);

\node [dot=right:$m_{1}$] at (x) {};

\end{tikzpicture}
\end{document}

However, I would like to add a dashed line from m onwards as in the picture, and I also like to highlight in red only the portion of the line through x* to m with positive values of the y-axis. Can I do it with \path? Is the rest of the code correct? Do you have any suggestions for improving it? I hope my question is clear. Thank you for your time and help.

Nenne
  • 697

2 Answers2

4

Most of this is from my previous answer.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc} 

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=2pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm,font=\small]

\draw[extended line,thick,<->] (0,0) -- (4.5,0) node[anchor=north west] {state 1};
\draw[extended line,thick,<->] (0,0) -- (0,4.5) node[anchor=south east] {state 2};

\coordinate (A) at (0,0);
\coordinate (B) at (4,-1);

\draw [extended line=2cm, <->] (A) -- (B) coordinate[midway] (M)
node[pos=1.3,sloped,above]{$X$};     

\draw [extended line,dashed, <->] ($(M)!3cm!270:(A)$) -- ($(M)!3cm!90:(A)$) 
%node[pos=0.9,right=1em]{$m=x^*+\varepsilon$ space of discount factors} 
coordinate[pos=0.2] (x);

\draw[blue,-latex] (0,0) -- (x) coordinate[pos=2](y) coordinate[pos=0.7](z1);
\draw[dashed] (x) -- (y) coordinate[pos=0.3](z2);
\foreach \X in {1,2}
{\draw ($(z\X)!3cm!270:(A)$) -- ($(z\X)!4cm!90:(A)$)
node[pos=0.1,right=1pt]{$p=\X$} (intersection cs:first
line={($(z\X)!3cm!270:(A)$) -- ($(z\X)!4cm!90:(A)$)},second line={(A)--(B)})
node[fill,inner sep=2pt,circle]{};
}

\node [red,dot=below left:$x^{*}$] at (M) {}; 
\node [red,dot=above left:$m_{1}$] at (x) {};
\end{tikzpicture}
\end{document}

enter image description here

If calc does not find the right intersections on your machine, you could try

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,intersections} 

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=2pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm,font=\small]

\draw[extended line,thick,<->] (0,0) -- (4.5,0) node[anchor=north west] {state 1};
\draw[extended line,thick,<->] (0,0) -- (0,4.5) node[anchor=south east] {state 2};

\coordinate (A) at (0,0);
\coordinate (B) at (4,-1);

\path[name path=AB,overlay] (A) -- (8,-2);
\draw [extended line=2cm, <->] (A) -- (B) coordinate[midway] (M)
node[pos=1.3,sloped,above]{$X$};     

\draw [extended line,dashed, <->] ($(M)!3cm!270:(A)$) -- ($(M)!3cm!90:(A)$) 
%node[pos=0.9,right=1em]{$m=x^*+\varepsilon$ space of discount factors} 
coordinate[pos=0.2] (x);

\draw[blue,-latex] (0,0) -- (x) coordinate[pos=2](y) coordinate[pos=0.7](z1);
\draw[dashed] (x) -- (y) coordinate[pos=0.3](z2);
\foreach \X in {1,2}
{\draw ($(z\X)!3cm!270:(A)$) -- ($(z\X)!4cm!90:(A)$)
coordinate[pos=0.3] (x\X)
node[pos=0.1,right=1pt]{$p=\X$};
\path[name path=aux\X,overlay]($(z\X)!3cm!270:(A)$) -- ($(z\X)!4cm!90:(A)$);
\path[name intersections={of=AB and aux\X,by=i\X}] (i\X)
node[fill,inner sep=2pt,circle]{};
}
\draw[red] (0,0) -- (x1);
\node [red,dot=below left:$x^{*}$] at (M) {}; 
\node [red,dot=above left:$m_{1}$] at (x) {};
\end{tikzpicture}
\end{document}
  • Much appreciated, even though your code is still difficult to understand for a novice like me. When I run your code, the black intersection points with X get both moved to the origin, why? – Nenne May 02 '19 at 16:13
  • @Nenne Which compiler do you use? And when was the last time you updated your TeX installation? I get this very output when running pdflatex on my code. –  May 02 '19 at 16:16
  • I'm using TeXworks, I updated my installation in december 2017... – Nenne May 02 '19 at 16:18
  • @Nenne Could you perhaps try to update it? For me it is hard to debug since I cannot reproduce the issue. –  May 02 '19 at 16:19
  • @Nenne This depends on which installation you are using. AFAIK TeXworks is not an installation. For instance, I use TeXLive, which comes with MacTeX and has an update manager. AFAIK there is only one other major installation which is MikTeX. (Sorry, I do not know much about these things.) –  May 02 '19 at 16:44
  • Ok, I think it got it, I have MikTeX installed. I know very little about everything in LateX! – Nenne May 02 '19 at 16:49
  • sorry for asking that again, is there a way to get those intersections without updating my installation? I really need to show them on the figure – Nenne Jun 21 '19 at 10:12
  • @Nenne I added an alternative. Does this one find the right intersections? –  Jun 21 '19 at 13:08
  • now it works fine, thank you. I was trying to modify the codes, and I was wondering how to add a vector from (0,0) to the p=1 line. I tried to add \ifnum inside the \foreach to define a coordinate, but it doesn't work. could you help me? – Nenne Jun 21 '19 at 13:28
  • and how did you guess \path[name path=AB,overlay] (A) -- (8,-2)? why exactly these numbers? – Nenne Jun 21 '19 at 13:44
  • @Nenne (8,-2) is 2 times (4,-1), I just extended the line by hand. And I added a proposal to draw a line from (0,0) to the p=1 line. Of course, it is a line so I had to make a choice, my choice was pos=0.3 but you can modify. –  Jun 21 '19 at 13:57
3

You can use \path and angle between two coordinates from here.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections} 
\makeatletter      
\newcommand{\getLengthAndAngle}[2]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}
                              {\pgfpointanchor{#2}{center}}
    \global\let\myangle\pgfmathresult % we need a global macro 
    \pgfpointdiff{\pgfpointanchor{#1}{center}}
                 {\pgfpointanchor{#2}{center}}
    \pgf@xa=\pgf@x % no need to use a new dimen
    \pgf@ya=\pgf@y
    \pgfmathparse{veclen(\pgf@xa,\pgf@ya)/28.45274} % to convert from pt to cm   
    \global\let\mylength\pgfmathresult % we need a global macro
}
\makeatother 
\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\draw[name path=state_x,extended line,thick,<->] (0,0) -- (4.5,0) node[anchor=north west] {\small state 1};
\draw[extended line,thick,<->] (0,0) -- (0,4.5) node[anchor=south east] {\small state 2};

\coordinate (A) at (0,0);
\coordinate (B) at (4,-1);

\draw [extended line=2cm, <->] (A) -- (B) coordinate[midway] (M)
node[pos=1.2,right=1em, font=\small]{$X$};     


\path [name path=mx,dashed, <->] ($(M)!3cm!270:(A)$) -- ($(M)!3cm!90:(A)$) node[pos=0.9,right=1em, font= \small]{$m=x^*+\varepsilon$ space of discount factors} coordinate[pos=0.2] (x);

\node [dot=right:$x^{*}$] at (M) {}; 
\fill [red] (M) circle [radius=2pt] ;


\coordinate (C) at (2,-1);
\coordinate (D) at (1,4);
\coordinate (P) at (3,0);


\draw [shorten >=-30pt,shorten <=-30pt] (C) -- (D) node [pos=1.2,right=1em, font=\small]{$p=1$};
\draw [shorten >=-30pt,shorten <=-30pt] (P) -- +($(D)-(C)$) node [pos=1.2,right=1em, font=\small]{$p=2$};
\draw[blue,-latex] (0,0) -- (x);
%%%%%%% CODE Added %%%%%%%%%%%%%%%%%%
\getLengthAndAngle{A}{x}
\draw [dashed](x) --++ (\myangle:\mylength);
\node [dot=left:$m_{1}$] at (x) {};
\path [name intersections={of=state_x and mx,by={xx}}];
\getLengthAndAngle{M}{x}
\draw [dashed] (x)--++( \myangle:\mylength);
\draw [dashed] (M)--++( \myangle+180:\mylength);
\draw [dashed] (M)--(xx);
\draw [dashed,red] (xx)--(x);
\end{tikzpicture}
\end{document}

enter image description here