0

I'm trying to achieve the following: enter image description here

I know I can do that with help of intersections tikz library. However, the main requirement is to avoid any temporary paths (\path[name path=t] (2,0) -- (2,\ymaxv);) and name them somehow. In big projects it becomes messy.

Here are two my attempts that unfortunately doesn't work:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture} \begin{axis}[axis lines = center] \def\ymaxv{\pgfkeysvalueof{/pgfplots/ymax}}

\addplot[blue, name path global=x] {x};

% this doesn't work \draw[red] (2,0) -- (intersection of {2,0--2,\ymaxv} and x);

% neither this does \draw[red, name intersections={of={2,0--2,\ymaxv} and x}] (2,0) -- (intersection-1);

\end{axis} \end{tikzpicture}

\end{document}

The first one (I know that is deprecated) doesn't see x path, but the other cannot work with 2,0--2,\ymaxv path.

antshar
  • 4,238
  • 9
  • 30
  • 1
    Why is it messy? Because you can't reuse path name? (define a macro that generates unique path name automatically?) – user202729 Dec 25 '21 at 11:55
  • Could you elaborate your problem a bit more precise, please. Because e.g. if you have a function and want to have a vertical line to the x-axis (as shown in the example) this could easily be done without the intersections library. – Stefan Pinnow Dec 26 '21 at 06:09
  • @StefanPinnow, yes, in general I want exactly what you described: having any x₀ coordinade, draw straight line from x-axis to the function. But I decided to simplify the question as much as I could. – antshar Dec 26 '21 at 13:14

2 Answers2

2

Here I present two possibilities to add vertical lines. For details please have a look at the comments in the code.

% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat` level or higher so for TikZ commands axis coordinates
    % are used by default
    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
    /pgf/declare function={
        % define your function
        f(\x) = (\x)^2;
        % (optionally defines some constants so you only have *one* place where
        %  you need to change it)
        a = 3;
    },
]
    \begin{axis}[
        no markers,
    ]
        \addplot {f(x)};
        % to have vertical lines to y = 0 simply use `ycomb`
        % (and works for arbitrary `\addplot`s, i.e. also table data etc.)
        \addplot+ [ycomb,samples at={2}] {f(x)};
        % to have vertical lines to arbitrary y-values you can e.g. use TikZ commands
        \draw [green] (a, {f(a)}) -- (a, 20);
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • I guess it'll work in most of my scenarios, but what about arbitrary paths? Sometimes I have to path with in=A,out=B arguments or even .. controls ... for even more curly lines. Is there any other approach that avoids creating temporal paths? – antshar Dec 26 '21 at 22:36
  • So now you are asking for arbitrary paths and not for known functions (any more). So the problem "reduces" to pure TikZ which then will also work in PGFPlots. Unfortunately I am not 100% sure if I have understood your question right. Therefore I suggest to ask a new follow-up question and describe you problem in more detail (what "intersects" with what). Because I'll don't check pure TikZ questions, please ping me here again with a link to the new question. Then I'll have look. – Stefan Pinnow Dec 26 '21 at 23:16
  • I created one: https://tex.stackexchange.com/q/628207/213149 – antshar Dec 27 '21 at 11:28
  • Sorry that I can't give a general answer, but at least for known functions you have one. So please consider accepting this answer (by clicking on the checkmark ✓) so it gets off the "unanswered list". – Stefan Pinnow Dec 27 '21 at 18:28
  • please don't treat me as ungrateful or rude, but the title of this question is about using explicit paths even though it's only an intermediate step for something greater I'm trying to achieve. So there is a chance, someone will respond to this the the exact answer. Otherwise, someone who's also interested in this specific question will be disappointed because "accepted answer" has nothing to do with using explicit paths. – antshar Dec 27 '21 at 18:43
  • Ok, to be more precise: I would change (at least) the title of the question so that it is related to explicit functions (as I have asked in the comment below the question. The new question is the general one. Otherwise one of both would be a duplicate of the other ... You could also add a link in the body of the question with your ultimate goal stated in the follow-up question. – Stefan Pinnow Dec 27 '21 at 20:19
1

Using the tzplot package:

enter image description here

\documentclass{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture} \tzaxes(-5,-5)(5,5) \tzticks*black!40{-4,-2,2,4}(-3pt:3pt){-4,-2,2,4} \tzticks<-3pt,-3pt>{-4,-2,2,4}{-4,-2,2,4} \def\Fx{\x} \tzfn[blue,thick]\Fx[-5:5] \tzvXpointat{Fx}{2}(X) \tzprojxred,solid,thick \end{tikzpicture}

\end{document}

I. Cho
  • 2,985