1

I need to output the absolute value function on the axis. Here is my code:

\documentclass[10pt,letterpaper,no-math]{article} 
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt
\usepackage{polynom} 
\usepackage{anyfontsize}
\usepackage{helvet}
\usepackage{mathpazo}

\renewcommand{\familydefault}{\sfdefault}
\usepackage{graphicx}

\usepackage{amssymb,amsmath}
\usepackage[slantfont,boldfont]{xeCJK} 
\setCJKmainfont{SimSun}
\usepackage{mathtools}
\usepackage{xcolor}
\usepackage{tikz} 
\usetikzlibrary{patterns}
\usepackage{polyglossia} 
\usepackage{unicode-math} 
\usepackage{chemfig}
\usepackage{scalerel}
\usepackage{tkz-euclide}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.14}

\usepackage{fontspec,kantlipsum}

\setmainfont
[    Extension = .otf,
   UprightFont = *-regular,
      BoldFont = *-bold,
    ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]{xits}

\DeclareMathSizes{9.8}{6}{4}{4}
\DeclareMathSizes{10.0}{9}{4}{4}
\DeclareMathSizes{10.95}{6}{4}{4}   
\DeclareMathSizes{11}{6}{4}{4}      
\DeclareMathSizes{12}{6}{14}{4}     

\parindent0em 
\pagestyle{empty} 
\setlength{\parindent}{0in}

\setmathfont
[  
    Extension = .otf, 
     BoldFont = *bold,
    Ligatures = TeX,
]{xits-math}
\usepackage{graphicx} 
\graphicspath{ {c:/Users/Administrator/AppData/collection.media/} }
\usepackage{array}
\newcommand\scalemath[2]{\scalebox{#1}{\mbox{\ensuremath{\displaystyle #2}}}}
\begin{document}

\begin{equation}
   \text{ $ y=\lvert x-2 \rvert+\lvert x-3 \rvert+\lvert x-4 \rvert=$} \left \{ \begin{aligned}
        3x-9, &x\geqslant 4,\\
        x-1, &3\leqslant x < 4,\\
        5-x, &2\leqslant x < 3,\\
        -3x+9, & x < 2. 
    \end{aligned} 
    \right.\
    \qquad
\end{equation}

\begin{figure}[!htb]
\centering
\begin{tikzpicture}[
    transform shape% <- added to scale nodes too
]
  \begin{axis}[
    xmin=-2,xmax=8,ymin=0,ymax=9,
    no markers,
    axis y line=middle, 
    axis x line=center,
    axis equal,
    ytick={-2,0,2,3,4,6,8}, % make steps of length 0.5
    xtick={0,2,3,4,6,8}, % make steps of length 5
    xlabel = {$x$},
    ylabel = {$y$},
  ] 
    \addplot +[samples=250,ultra thick] {abs(x-2)+abs(x-3)+abs(x-4)};

\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

The output of this function should be left-right symmetric, but strangely, the image after x is greater than 5 disappears.

enter image description here

I checked the range of values for x and it looks like everything is fine. Can someone tell me where the problem is?

2

If you plot the function over a symmetric domain of the form 3-X:3+X with some positive number X, you will more easily appreciate its symmetry.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.14}

\begin{document}
\begin{tikzpicture}[
    transform shape% <- added to scale nodes too
]
  \begin{axis}[
    xmin=-2,xmax=8,ymin=0,ymax=9,
    no markers,
    axis y line=middle, 
    axis x line=center,
    axis equal,
    ytick={-2,0,2,3,4,6,8}, % make steps of length 0.5
    xtick={0,2,3,4,6,8}, % make steps of length 5
    xlabel = {$x$},
    ylabel = {$y$},
    domain=0.5:5.5
   ] 
    \addplot +[samples=250,ultra thick] {abs(x-2)+abs(x-3)+abs(x-4)};

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

BTW, most of your preamble is not related to the problem.

ADDENDUM: On the symmetry.

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.14}

\begin{document}
Consider the function
\begin{equation}
    y~=~\lvert x-2 \rvert+\lvert x-3 \rvert+\lvert x-4 \rvert~=~
    \begin{dcases}
        3x-9, &x\ge 4\;,\\
        x-1, &3\le x < 4\;,\\
        5-x, &2\le x < 3\;,\\
        -3x+9, & x < 2\;. 
    \end{dcases} 
\end{equation}
It is symmetric under $x\mapsto 3-x$, see Figure~\ref{fig:f}. To see that more
easily, define $x':=x-3$ (such that $x=3+x'$). Then the function becomes
\begin{equation}
 y~=~\lvert x'+1 \rvert+\lvert x'\rvert+\lvert x'-1 \rvert
 ~=~\lvert 1+x' \rvert+\lvert x'\rvert+\lvert 1-x' \rvert\;,
\end{equation}
which is obviously symmetric under $x'\mapsto -x'$. To appreciate this symmetry,
we might want to take the domain symmetric in $x'$, which corresponds to an
interval $I=[3-\Delta,3+\Delta]$ with some $\Delta>0$.

\begin{figure}[!htb]
\centering
\begin{tikzpicture}[
    transform shape% <- added to scale nodes too
]
  \begin{axis}[
    xmin=-2,xmax=8,ymin=0,ymax=9,
    no markers,
    axis y line=middle, 
    axis x line=center,
    axis equal,
    ytick={-2,0,2,3,4,6,8}, % make steps of length 0.5
    xtick={0,2,3,4,6,8}, % make steps of length 5
    xlabel = {$x$},
    ylabel = {$y$},
    domain=0.5:5.5
   ] 
    \addplot +[samples=250,ultra thick] {abs(x-2)+abs(x-3)+abs(x-4)};
   \draw[dashed] (3,0) -- (3,8) node[above]{symmetry axis};
\end{axis}
\end{tikzpicture}
\caption{The function has a reflection symmetry.}
\label{fig:f}
\end{figure}

\end{document}

enter image description here

  • Still do not understand, why take a symmetric domain? Isn't it possible to use x in the range? – mcmxciv Nov 23 '18 at 4:56
  • 1
    @mcmxciv I tried to make it clearer by adding an appendix. (This is more a math than TeX problem.) – marmot Nov 23 '18 at 5:03

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.