4

How to scale down the size of tick (j-1, j and j+1) of a graph in Tikz?

Moreover, as I don't have a lot of experience with Tikz, if there is a more standard/efficient way of writing the whole graph, I'm obviously glad to learn!

I obtained this graph by modifying a an example that I found on Texample.net

graph

\documentclass[a4paper, 12pt]{book}
\usepackage{pgfplots, tikz}
\begin{document}
\begin{tikzpicture}[scale = 0.7]
% Draw axes
\draw [<->,thick] (0,5) node (yaxis) [above] {$y$}
|- (5,0) node (xaxis) [right] {$x$};

\coordinate (c) at (2,2);
\fill[red] (c) circle (3pt);
\draw[dashed] (yaxis |- c) node[left] {$i$}
-| (xaxis -| c) node[below] {$j-1$};

\coordinate (d) at (3,2);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {$i$}
-| (xaxis -| d) node[below] {$j$};

\coordinate (d) at (3,3);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {$i+1$}
-| (xaxis -| d) node[below] {$j$};

\coordinate (d) at (4,2);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {$i$}
-| (xaxis -| d) node[below] {$j+1$};

\draw [thin, gray] (0,0) grid (5,5);
\end{tikzpicture}
\end{document}
ecjb
  • 883

2 Answers2

3

I put some opacity in the grid. In my opinion, they should also put the red dots in the foreground to hide the dotted lines that invade the dots. But I don't know if you want this.

enter image description here

\documentclass[a4paper, 12pt]{book}
\usepackage{pgfplots, tikz}
\begin{document}
\begin{tikzpicture}[scale = 0.7]
% Draw axes
\draw [<->,thick] (0,5) node (yaxis) [above] {$y$}
|- (5,0) node (xaxis) [right] {$x$};

\coordinate (c) at (2,2);
\fill[red] (c) circle (3pt);
\draw[dashed] (yaxis |- c) node[left] {}
-| (xaxis -| c) node[below] {$\scriptstyle{j-1}$};

\coordinate (d) at (3,2);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {$\scriptstyle{i}$}
-| (xaxis -| d) node[below] {};

\coordinate (d) at (3,3);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {$\scriptstyle{i+1}$}
-| (xaxis -| d) node[below] {$\scriptstyle{j}$};

\coordinate (d) at (4,2);
\fill[red] (d) circle (3pt);
\draw[dashed] (yaxis |- d) node[left] {}
-| (xaxis -| d) node[below] {$\scriptstyle{j+1}$};

\draw [thin, gray] (0,0) grid (5,5);
\end{tikzpicture}
\end{document}
Sebastiano
  • 54,118
  • 3
    Hi Sebastiano, happy new year! You could also add every node/.style={scale = 0.7} to scale down all nodes at once – caverac Jan 01 '19 at 13:07
  • @caverac Hi to you and a happy new year full of joy and serenity for you and your loved ones. You can make all the changes you want, I don't understand English very much :( especially in the questions without figures. – Sebastiano Jan 01 '19 at 13:11
  • 1
    Happy new year! thanks to Sebastiano and @caverac. Where in the code should every node/.style={scale = 0.7} be written? – ecjb Jan 01 '19 at 13:11
  • 2
    @ecjb It would be at the same location where you specify the scale, it would be something like \begin{tikzpicture}[scale = 0.7, every node/.style={scale = 0.7}]. I think your answer is spot on Sebastiano, just wanted to give you another option :) – caverac Jan 01 '19 at 13:14
  • Please note that \scriptstyle is a switch (just like \tiny or \small) and does not take an argument. – samcarter_is_at_topanswers.xyz Jan 01 '19 at 13:37
2

Scaling elements that contain text is not a good idea, it will result in a suboptimal choice of letter shapes, see Why not scale elements that contain text for more information. Instead of scaling the tick labels, better choose an appropriate font size, e.g. \scriptsize. In case you want specific elements, like x and y to remain in normal size, you can add \normalsize in their nodes.

Some other comments:

  • you print the labels for i and j three times above each other, depending on the rendering precision of your pdf viewer this can cause the font to look awkward, better only print it one time.

  • It would also be better, if you would choose unique names for your coordinates instead of re-using d for 3 of them. This has the big advantage, that you can easily control the order in which you draw things. For example you can first draw the grid in the background, then the axis, the dashed lines and the red dots at the end.

  • To avoid the problem that several dashed with different dash positions are overprinted you could either restrict the drawing range (as done in the example below) or invert the drawing direction to ensure that they all have the same starting point.


\documentclass[a4paper, 12pt]{book}
\usepackage{pgfplots, tikz}
\begin{document}

\begingroup
    \scriptsize
    \begin{tikzpicture}[scale=0.7]

        % grid
        \draw [thin, gray!30] (0,0) grid (5,5);

        % Draw axes
        \draw [<->,thick] (0,5) node (yaxis) [above] {\normalsize $y$} |- (5,0) node (xaxis) [right] {\normalsize $x$};

        % Coordinates   
        \coordinate (a) at (2,2);
        \coordinate (b) at (3,2);
        \coordinate (c) at (3,3);
        \coordinate (d) at (4,2);

        % Dashed lines
        \draw[dashed] (yaxis |- a) node[left] {$i$} -| (xaxis -| a) node[below] {$j-1$};
        \draw[dashed] (a |- b) -| (xaxis -| b) node[below] {$j$};
        \draw[dashed] (yaxis |- c) node[left] {$i+1$} -| (b -| c);
        \draw[dashed] (b |- d) -| (xaxis -| d) node[below] {$j+1$};

        % red dots
        \fill[red] (a) circle (3pt);
        \fill[red] (b) circle (3pt);
        \fill[red] (c) circle (3pt);
        \fill[red] (d) circle (3pt);

    \end{tikzpicture}
\endgroup
\end{document}

enter image description here