Tx · LaTeX & TikZ

How to Make a Variation Table and Graph of ln(x) in TikZ

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the function

    Use the natural logarithm ln(x), whose domain is x greater than zero.

  2. 2. State its variation

    Since its derivative is 1/x, ln(x) increases throughout its domain, from negative infinity to positive infinity.

  3. 3. Write the table

    Show the endpoints 0+ and positive infinity, with an increasing arrow between the function values.

  4. 4. Plot the curve

    Use PGFPlots, which builds on TikZ, to draw ln(x) over a positive x-range.

The natural logarithm, written ln⁡x\ln x, is defined only for x>0x>0. Its variation table is short: the function increases from −∞-\infty to +∞+\infty. The example below typesets that table and draws the graph using PGFPlots, a plotting package built on TikZ.

The code is a complete LaTeX document. Compile it with a current LaTeX distribution that includes PGFPlots.

Complete LaTeX example

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\[
\begin{array}{c|ccc}
x & 0^+ & & +\infty \\
\hline
\ln x & -\infty & \nearrow & +\infty
\end{array}
\]

\begin{center}
\begin{tikzpicture}
  \begin{axis}[
    width=10cm,
    height=7cm,
    axis lines=middle,
    xmin=0,
    xmax=4,
    ymin=-3,
    ymax=2,
    xlabel={$x$},
    ylabel={$y$},
    xtick={1,2,3,4},
    ytick={-3,-2,-1,0,1,2},
    grid=both,
    domain=0.08:4,
    samples=200,
    unbounded coords=jump
  ]
    \addplot[blue,thick] {ln(x)};
  \end{axis}
\end{tikzpicture}
\end{center}

\end{document}

The only package needed for the graph is pgfplots; it loads TikZ as a dependency. The compat setting selects PGFPlots’ compatibility behavior. If your installation is older and does not support version 1.18, change the setting to a version available in that installation.

Why the table has this form

The domain of ln⁡x\ln x is (0,∞)(0,\infty). On that domain,

ddxln⁡x=1x>0.\frac{d}{dx}\ln x=\frac{1}{x}>0.

A positive derivative means the function is strictly increasing everywhere it is defined. The endpoint behavior is

lim⁡x→0+ln⁡x=−∞,lim⁡x→+∞ln⁡x=+∞.\lim_{x\to 0^+}\ln x=-\infty, \qquad \lim_{x\to +\infty}\ln x=+\infty.

In the array, the blank middle cell separates the two endpoint columns so the arrow can sit between them. The 0+0^+ notation matters: the logarithm is not defined at zero, and the curve approaches the vertical axis without touching it.

How the graph settings work

The axis environment sets the visible window. Here, the horizontal range is from 0 to 4 and the vertical range is from −3-3 to 2. The curve itself is drawn only for 0.08≤x≤40.08\le x\le4, because the logarithm cannot be evaluated at zero. The chosen lower plotting limit keeps the curve visible near the vertical axis; it does not change the function’s domain.

The expression {ln(x)} is PGFPlots’ natural-log function. With samples=200, PGFPlots evaluates it at many points and joins them to make a smooth curve. The curve passes through (1,0)(1,0) because ln⁡1=0\ln 1=0, and through (e,1)(e,1) because ln⁡e=1\ln e=1. The axis lines=middle option places the horizontal axis at y=0y=0 and the vertical axis at x=0x=0.

If you want to label the curve, add a node to the plot, for example:

\addplot[blue,thick] {ln(x)}
  node[pos=0.7,above] {$y=\ln x$};

Put this in place of the original \addplot line. The pos value controls where along the plotted curve the label appears.

If you mean log base a

For y=log⁡axy=\log_a x, where a>0a>0 and a≠1a\ne1, the domain remains x>0x>0. If a>1a>1, the function increases, and its limits at the endpoints are −∞-\infty and +∞+\infty, just as for ln⁡x\ln x. If 0<a<10<a<1, it decreases instead: it tends to +∞+\infty as x→0+x\to0^+ and to −∞-\infty as x→+∞x\to+\infty.

PGFPlots’ ln(x) draws the natural logarithm. To plot a logarithm with another base, use the change-of-base formula log⁡ax=ln⁡x/ln⁡a\log_a x=\ln x/\ln a, so the plot expression becomes {ln(x)/ln(a)} with a numerical value substituted for a.

Common mistakes

  • Plotting at x=0x=0: The logarithm is undefined there. Start the plotted domain at a small positive number instead.
  • Using a decreasing arrow for ln⁡x\ln x: Its derivative is positive on its entire domain, so the variation arrow points upward as xx increases.
  • Confusing the plot window with the domain: A graph shown only up to x=4x=4 is still a graph of a function defined for every x>0x>0.
  • Treating log and ln as interchangeable in code: In PGFPlots, use ln(x) for the natural logarithm. For a different base, apply the change-of-base formula.
  • Expecting the curve to cross the vertical axis: The line x=0x=0 is an asymptote, not part of the logarithm’s graph.

References

  1. PGFPlots: Create normal/logarithmic plots in LaTeX

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.