在 LaTeX 中绘制截断的线性方程

Plotting truncated linear equations in LaTeX

我正在写我的硕士论文,我想在 LaTeX 中创建下图:

到目前为止,这是我的代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\usepackage{pgfplots}
\usepackage[active,float]{preview}

\PreviewEnvironment{tikzpicture}

\begin{document}

\begin{tikzpicture}[scale=1.5]
    %axis
    \draw (0,0) -- coordinate (x axis mid) (4,0);
        \draw (0,0) -- coordinate (y axis mid) (0,3);

        \foreach \x in {0,...,2}
        \draw (\x,0pt) -- (\x,-3pt)     node[anchor=north] {\x};
        \foreach \y in {0,...,1}
            \draw (0pt,\y) -- (-3pt,\y)     node[anchor=east] {$\gamma_{max}$}; 

    % draw lines
    \draw [blue,thick](0,0) coordinate (a_1) -- (2,1) coordinate (a_2); 
    \draw [blue,thick](2,1) coordinate (b_1) -- (3,1) coordinate (b_2); 

    \draw[black,dotted,thick] (a_2|-o) -- (b_1);
    %labels      
    \node [below=0.6cm] at (x axis mid) {\textbf{N$_i$(x$_c$)$\;\;\longrightarrow$}};
    \node [left=0.5cm] at (y axis mid) {$f_1$(x$_c$)};
\end{tikzpicture}

\end{document}

我的问题如下:

此源代码

\documentclass[tikz,multi=false,border=2mm]{standalone}

\begin{document}

\begin{tikzpicture}[scale=1.5]
    %axes
    \draw (0,0) -- (3.5,0);
    \draw (0,0) -- (0,2);
    \foreach \x in {0,2} \draw (\x,0pt) -- (\x,-3pt);
    \foreach \y in {.5,1.5} \draw (0pt,\y) -- (-3pt,\y);
    % draw lines
    \draw [blue,thick] (0,.5) -- (2,1.5) -- (3,1.5);
    \draw [black,dotted,thick] (2,1.5) -- (2,0);
    %labels
    \node at (0,-.25) {[=10=]$};  %%  default
    \node at (2,-.25) {$\mathsf{Q}_2$};
    \node [below=6mm] at (2,0) {$\mathsf{N_i(x_c)}\;\;\longrightarrow$};
    \node at (-.25,.5) {$\mathsf{0}$};  %%  sans serif
    \node at (-.4,1.5) {$\gamma_{max}$};
    \node at (-1,1.1) {$\uparrow$};
    \node at (-1,.8) {$\mathsf{f_1(x_c)}$};
\end{tikzpicture}

\end{document}

产生以下输出:

尽管我对第 23 和 24 行中 y 轴标签的解决方案可能非常粗糙。

此外,我认为在数学模式下管理衬线和无衬线字体可以比我做的更有条理:我的代码中有几处可以改进,但我认为你有能力甚至只使用其中的一部分。

最后,如果您愿意,可以使用 very thick 选项绘制蓝线(而不仅仅是 thick)。

我建议使用 PGFPLOTS:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro{\ymax}{1}
    \pgfmathsetmacro{\q}{4}
    \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xlabel=$N_i(x_c)$,
        ylabel=$f_1(x_c)$,
        xmin=0, xmax=\q+2.5,
        ymin=-.5, ymax=\ymax+.5,
        xtick={0,\q},
        ytick={0,\ymax},
        xticklabels={0,$Q_2$},
        yticklabels={0,$\gamma_{max}$},
        ]
        \addplot [domain=0:\q,blue,very thick] {x*(\ymax/\q)} coordinate (q);
        \addplot [domain=\q:\q+2,blue,very thick] {\ymax};
        \draw[thick, dashed] (axis cs:\q,-.5) -- (q);
    \end{axis}
\end{tikzpicture}
\end{document}