0

I have some points defined via

\coordinate (A1) at (-4,0);
\coordinate (B1) at (-2,1);
\coordinate (C1) at (0,0);
...

And I want to place a little dash at their location. I tied two approaches, but both throw me a bunch of errors

\foreach \p in {A1,B1,C1,A2,B2,C2,A3,B3,C3}{
    \draw[thick] ([yshift=-2pt]\p) -- ([yshift=2pt]\p);
}
\foreach \p in {A1,B1,C1,A2,B2,C2,A3,B3,C3}{
    \draw[thick] ($(\p)+(0,1)$) -- ($(\p)+(0,-1)$);
}

­

Here is MWE if you want to reproduce the error

\documentclass{standalone}

\usepackage{pgfplots} \pgfplotsset{compat=newest} \usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture} \begin{axis} [ axis lines = center, ymin=-1, ymax=2, xmin=-6, xmax=10, width=9cm, clip=false ]

\coordinate (A1) at (-4,0); \coordinate (B1) at (-2,1); \coordinate (C1) at (0,0);

\foreach \p in {A1,B1,C1}{ \draw[thick] ($(\p)+(0,1)$) -- ($(\p)+(0,-1)$); }

\end{axis} \end{tikzpicture}

\end{document}

antshar
  • 4,238
  • 9
  • 30
  • 1
    Both cases work as expected. Howeve, it is unknown, how you use your code fragments. So, please, be so kind and show us an MWE (in form of small but complete document). which reproduce your problem. – Zarko Jun 22 '21 at 11:01
  • @Zarko I didn't attach MWE, because you literally don't need anything to get the error. I was hoping for someone's knowledge about tikz that would immediately noticed what's wrong. Anyway, I've just added MWE for you. – antshar Jun 22 '21 at 11:33
  • Your code fragments haven't any error. Error is their use in your document. But, this is unknown (so far). Now I see, that you not use tikz but pgfplots. At he later you cant use \foreach loops on your way. I wonder, why you use pgfplots, wha˙t is purpose of this small lines, etc. They can be replaced wit some mark and defined something like `\addtplot [coordinates only, mark=|] coordinates {(<coordinate 1>) (<coordinate 2>) ... (ycoordinate n>)} – Zarko Jun 22 '21 at 11:53
  • 1
    @antshar: How could anyone have known that you use PGFPlots before your MWE!?? You are waisting peoples time, whenever you deside not to include a MWE. – hpekristiansen Jun 22 '21 at 11:53
  • @Zarko Don't you get errors when you run the MWE? In fact, despite not having tikz package as far as I know, pgfplots includes it, so there should be not any issues with \foreach. – antshar Jun 22 '21 at 11:58
  • @antshar, you are wrong. See my answer below. Both packages have a lot in common but they are not the same. BTW, your question is not clear at all. – Zarko Jun 22 '21 at 12:13

2 Answers2

3

As far I I can guess, you looking for the following:

enter image description here

(desired short lines are emphasized by red color)

pgfplots is based on tikz, however, all stuff from tikz doesn't work stragforward in \pgfplots. One of such cases is \foreach loop. For details see Manual for Package PGFPLOTS, section 8.1 Utility Commands, page 546.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usetikzlibrary{plotmarks}

\begin{document} \begin{tikzpicture} \begin{axis}[width=9cm, axis lines = center, xmin=-6, xmax=10, ymin=-1, ymax=2, clip=false ] \addplot [only marks, mark options={very thick, color=red, mark=|}] coordinates {(-4,0) (-2,1) (0,0)}; \end{axis} \end{tikzpicture} \end{document}

Zarko
  • 296,517
1

You can not use \foreach in every case inside axis. Se the manual p. 546-548. Here you can use \pgfplotsinvokeforeach like this:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
ymin=-1,
ymax=2,
xmin=-6,
xmax=10,
width=9cm,
clip=false
]
\coordinate (A1) at (-4,0);
\coordinate (B1) at (-2,1);
\coordinate (C1) at (0,0);
\pgfplotsinvokeforeach {A1,B1,C1}{
\draw[thick] ($(#1)+(0,1)$) -- ($(#1)+(0,-1)$);}
\end{axis}
\end{tikzpicture}
\end{document}

Graph with lines

Edit:

The calculated coordinate above are wrong.

Doing calc inside axis is also not straight forward - see: Calculation of coordinates for TikZ annotations in PGFplots axis environment

This works:

\pgfplotsinvokeforeach {A1,B1,C1}{ \draw[thick] ([yshift=-2pt]#1) -- ([yshift=2pt]#1);}