使 tikz 图片适合文本宽度

使 tikz 图片适合文本宽度

我曾经寻求帮助使用 Tikz 绘制一些图表,符合我需求的答案用于\begin{tikzpicture}[scale=1.1, every node/.style={scale=1.3}]设置环境,并且一些图表不适合我的文档的文本宽度,我尝试更改选项,[\width=\textwidth, every node/.style={\width=\textwidth}]但我收到以下错误

Package pgfkeys Error: I do not know the key '/tikz/width', to which you passed '\textwidth ', and I am going to ignore it. Perhaps you misspelled it. ...th, every node/.style={width=\textwidth}]

即使我得到了具有所需输出的 ​​PDF

怎样做才正确?

    \documentclass[11pt,a4paper]{scrreprt}
    %bigger margins than usual
    \areaset{336pt}{705pt}
    \usepackage{tikz}
    \usepackage{amsmath}
    \usetikzlibrary{automata,calc,patterns,decorations.pathmorphing,decorations.markings}
        \tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}]
    \tikzstyle{damper}=[thick,decoration={markings,  
        mark connection node=dmp,
        mark=at position 0.5 with 
        {
            \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=15pt,minimum height=3pt,draw=none] {};
            \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
            \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
        }
    }, decorate]
    \tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=0.75cm,minimum height=0.3cm,inner sep=0pt,outer sep=0pt]
\begin{document}
    \begin{center}
        \begin{tikzpicture}[scale=1, every node/.style={scale=1.3}]

\node [draw, outer sep=0pt, thick] (M) [minimum width=2cm, minimum height=1cm,yshift=-1cm] {$\mathrm{m}$};

\node (ground) [ground,anchor=north,yshift=3.3cm,minimum width=4cm,xshift=0cm] at (M.north) {};
\node [outer sep=0pt, thick] (ground2) [minimum width=2cm, minimum height=1cm, yshift =3cm] {};

\draw (ground.south east) -- (ground.south west);


\draw [spring] (ground2.south) -- (M.north) node [midway,left] {$\mathrm{k}$};

\draw [-latex,ultra thick] (M.north west) ++ (-0.5cm, 0.0cm) -- +(0cm,-1cm);
\draw [dashed] (M.north west) ++ (0cm, 0cm) -- +(-0.8cm,0cm);
\node (y1) at (M.north west) [xshift = -0.7cm, yshift = -0.8cm] {$x$};

\draw [dashed] (M.north west) ++ (-0.8cm, 0.5cm) -- +(14cm,0cm);
\node (y2) at (M.north west) [align=center,xshift = -0.8cm, yshift = 0.4cm] {Posición\\original};



\end{tikzpicture}
    \end{center}
\end{document}

我必须说我假装在图形环境中使用此图表

答案1

您可以使用该adjustbox包来缩放任何内容:

\usepackage{adjustbox}

\begin{adjustbox}{width=\textwidth}
\begin{tikzpicture}[....]
...
\end{tikzpicture}
\end{adjustbox}

这会将 tikzpicture 缩放为框,包括文本在内的所有内容。

答案2

“cfr”建议tikzscale在另一个答案的评论中推荐了这个包。我花了一段时间才弄清楚如何使用它,所以这里是一个总结。文档在这里

  1. 添加\usepackage{tikzscale}到序言中。
  2. 将您的tikzpicture环境(包括\begin{tikzpicture}[...])移至单独的*.tikz文件(例如diagram.tikz)。
  3. 使用\includegraphics[width=\textwidth]{diagram.tikz}

例子:

\documentclass{article}                                                         
\usepackage[showframe,nomarginpar]{geometry}                                    
\usepackage{tikz}                                                               
\usepackage{tikzscale}                                                          
                                                                                
\begin{document}

Inline Tikzpicture:

\noindent\begin{tikzpicture}[yscale=0.7,xscale=\textwidth/2cm]                  
  \draw [color=blue] plot[domain=0:2,variable=\x,samples=51,smooth] (\x,{sin(deg(\x*pi))}) ;
  \draw [color=red!30] plot[domain=0:2,variable=\x,samples=51,smooth] (\x,{-1.0*sin(deg(\x*pi))}) ;
  \coordinate (A) at (0,0);                                                     
  \coordinate (B) at (2,0);                                                     
  \draw (A) node {\textbullet} -- (1,0) node[above] {A} (1,0) node {\textbullet} -- (B) node {\textbullet};
\end{tikzpicture}                                                               

Using tikzscale:

\noindent\includegraphics[width=\textwidth]{diagram.tikz}
\end{document}

然后diagram.tikz包含:

\begin{tikzpicture}[yscale=0.1]
  \draw [color=blue] plot[domain=0:2,variable=\x,samples=51,smooth] (\x,{sin(deg(\x*pi))}) ;
  \draw [color=red!30] plot[domain=0:2,variable=\x,samples=51,smooth] (\x,{-1.0*sin(deg(\x*pi))}) ;
  \coordinate (A) at (0,0);
  \coordinate (B) at (2,0);
  \draw (A) node {\textbullet} -- (1,0) node[above] {A} (1,0) node {\textbullet} -- (B) node {\textbullet};
\end{tikzpicture}

结果: 在此处输入图片描述

几点注意事项:

  • 您不能同时设置两者[width=...,height=...]。只能设置一个。这是一个限制。我正在使用\begin{tikzpicture}[yscale=...]它来克服这个问题。
  • 您可以使用任何文件扩展名,但如果您使用 .tikz、.TIKZ、.TikZ、.pgf、.PGF 中的任何一个,则无需将其包含在语句中\includegraphics

相关内容