1

For one of my academic courses, i need to generate sample lattices and was wondering if there was any way to generalize the hard-coded points I am using. Instead of repeating -5.19,-3.46,-1.73,0,1.73,3.46,5.19 all the time, is there a way to define a macro or equivalent.

    \documentclass{standalone}

    \usepackage{tikz}

    \usepackage{pgfplots}
    \pgfplotsset{width=5.5in,compat=1.10}
    \begin{document}
      \begin{tikzpicture}
      \begin{axis}[axis lines=middle,
      xmin=-6, xmax=6,
            ymin=-6,ymax=6,
                    xlabel = $x$,
                    ylabel = $y$]
    \addplot [->, thick,  red]
            coordinates { (0,0) (sqrt(3),1)} node[above right,pos=1] {$v_1$};
    \addplot [->, thick,  blue]
            coordinates { (0,0) (0,2)} node[above right,pos=1] {$v_2$};

    \addplot+ [mark=none,black] table {
    0.5 1
    -0.5 1
    -1 0
    -0.5 -1 
    0.5 -1
    1 0
    0.5 1
    };

    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x + 2};
    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x-2};

    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x-4};
    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x+4};

    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x-6};

    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x+6};

    \addplot[only marks, mark=o, samples at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x};
    \end{axis}
    \end{tikzpicture}
    \end{document}

enter image description here

1
  • Sure, with \pgfplotsinvokeforeach ... – user121799 Mar 12 '19 at 3:05
0

You could use a loop. (Of course there are even simpler ways to create such lattices.)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{width=5.5in,compat=1.10}
\begin{document}
  \begin{tikzpicture}
  \begin{axis}[axis lines=middle,
  xmin=-6, xmax=6,
        ymin=-6,ymax=6,
                xlabel = $x$,
                ylabel = $y$]
\addplot [->, thick,  red]
        coordinates { (0,0) (sqrt(3),1)} node[above right,pos=1] {$v_1$};
\addplot [->, thick,  blue]
        coordinates { (0,0) (0,2)} node[above right,pos=1] {$v_2$};

\addplot+ [mark=none,black] table {
0.5 1
-0.5 1
-1 0
-0.5 -1 
0.5 -1
1 0
0.5 1
};
\pgfplotsinvokeforeach{-6,-4,...,6}{
\addplot[only marks, mark=o, samples
at={-5.19,-3.46,-1.73,0,1.73,3.46,5.19}]{-0.55*x + #1};}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.