1

In this case, I would like to plot some points from the graph of x^2 - x - 6 using foreach (coordinate in the MWE). Unfortunately, when I plug the foreach loop, the error stated

Undefined control sequence. \end{axis}

And, I've used these reference in advance to help with my case, yet doesn't yield anywhere to the solution :

(Actually, there're more but I forgot the link, though).

Here's my code, hopefully someone be able to help it.

\documentclass{article}

\usepackage{amsmath, amsthm, amsfonts, amssymb} \usepackage[framemethod=tikz]{mdframed} \usepackage{tikz} \usepackage{pgfplots} \usepackage{mathrsfs} \pgfplotsset{compat=newest}

\mdfdefinestyle{mdgreenbox}{ skipabove = 8pt, skipbelow = 8pt, linewidth = 2pt, rightline = false, leftline = true, topline = false, bottomline = false, linecolor = green, backgroundcolor = green!5, }

\begin{mdframed}[style=mdgreenbox] Lorem ipsum... {\centering \begin{tikzpicture}[] \begin{axis}[ x = .7cm, y = .7cm, axis lines = middle, xlabel = $x$, ylabel=$y$, xtick = {-5, 0, 5, 10}, ytick = {-15, -10, ..., 5}, minor tick num = 4, xmin = -3.3, xmax = 4.3, ymin = -7.3, ymax = 5.3, font = \tiny ] \draw[black, thin, smooth, domain = -7 : 12] plot (\x, {(\x)^2 - 1 * (\x) - 6});

        % Here...
        \foreach \x/\y in {-3/4, -2/0, -1/-4, 0/-6, 1/-6, 2/-4, 3/0, 4/6}{
            \node at (\x, \y) {\textbullet};
        }
    \end{axis}
\end{tikzpicture}\par}

\end{mdframed}

P.S. Also, is there anyway, so for each graph, I could use a definite foreach code to plot my points?

Wilory Lu
  • 101
  • 1
    Well, you're nesting a TikZ picture in another one, which is definitely not recommended. – egreg Sep 17 '23 at 13:04
  • I've tried to undo the nestings, but still is not solved.

    Before, I used to use that code structure to design my graph though.

    – Wilory Lu Sep 17 '23 at 13:07
  • 2
    Do \node foreach \x/\y in {-3/4, -2/0, -1/-4, 0/-6, 1/-6, 2/-4, 3/0, 4/6} [circle, fill, inner sep=+0pt, minimum size=+2.5pt] at (\x, \y) {}; or \fill[radius=1.25pt] foreach \x/\y in {-3/4, -2/0, -1/-4, 0/-6, 1/-6, 2/-4, 3/0, 4/6} {(\x, \y) circle[]}; The problem, yet again, was a (grouped) loop inside the axis environment. This makes trouble oftentimes. – Qrrbrbirlbel Sep 17 '23 at 13:24
  • Woah! It worked like charm! Thank you. – Wilory Lu Sep 17 '23 at 13:31
  • 1
    If you declare function={f(\x)=(\x)^2 - 1 * (\x) - 6;} as an option to the tikzpicture environment, you can replace (\x, {(\x)^2 …}) by (\x, {f(\x)}) and the points can then be placed a bit easier, too: \fill[radius=1.25pt] foreach \x in {-3, -2, ..., 4} {(\x, {f(\x)}) circle[]};. — By the way, is there a reason you use the axis environment from the PGFPlots package but not its \addplot command? – Qrrbrbirlbel Sep 17 '23 at 13:31
  • So, if I may conclude, the foreach loop I made should not be a nested but it should like inline or sort of like key from either \fill or \node, is it? – Wilory Lu Sep 17 '23 at 13:32
  • There's nothing in particular, I'm just using what I've used to. But, will do for the future. Thank you. – Wilory Lu Sep 17 '23 at 13:35

0 Answers0