2

I asked for help once to draw some diagrams using Tikz, the answer that suited my needs used \begin{tikzpicture}[scale=1.1, every node/.style={scale=1.3}] to set the enviroment, and some diagrams doesn't fit the textwidth of my document, I tried to changed the options to [\width=\textwidth, every node/.style={\width=\textwidth}] but I get the following error

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}]

even though I get a PDF with the desired output

What is the right way to do what I stated before?

    \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}

I must say that I pretend to use this diagram inside a figure enviroment

Paul Lara
  • 727
  • Please provide a MWE that can reproduce your problem. This way nobody can help you except of some general answers like tjhese here: https://tex.stackexchange.com/questions/4338/correctly-scaling-a-tikzpicture – koleygr Jan 27 '18 at 06:39
  • 1
    from your fragments is difficult to figured out how we can help you. the simplest is include image as pdf graphics like includegraphics[width=\textwidth]{...} – Zarko Jan 27 '18 at 06:39
  • \width=\textwidth would be wrong as tikzkey. width=\textwidth would be right, but AFAIK it doesn't has a width key. – Martin Scharrer Jan 27 '18 at 07:23
  • I´ve updated my question with a MWE – Paul Lara Jan 27 '18 at 08:31
  • related question: https://tex.stackexchange.com/questions/411671/tikz-how-to-set-width-of-complicated-tikz-image-to-textwidth/411680#411680 – Ignasi Jan 29 '18 at 08:52

2 Answers2

8

You can use the adjustbox package to scale any content:

\usepackage{adjustbox}

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

This scales the tikzpicture as box, so everything including the text.

Martin Scharrer
  • 262,582
0

"cfr" suggested the tikzscale package in a comment of the other answer. It took me a while to figure out how to use this, so here's a summary. Documentation here

  1. Add \usepackage{tikzscale} to the preamble.
  2. Move your tikzpicture environment (including \begin{tikzpicture}[...]) into a seperate *.tikz file (e.g. diagram.tikz).
  3. Use \includegraphics[width=\textwidth]{diagram.tikz}

Example:

\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(\xpi))}) ; \draw [color=red!30] plot[domain=0:2,variable=\x,samples=51,smooth] (\x,{-1.0sin(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}

Then diagram.tikz contains:

\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}

The result: enter image description here

A few note:

  • You cannot set both [width=...,height=...]. Only one. That's a limitation. I am using \begin{tikzpicture}[yscale=...] to overcome that.
  • You can use any file extension, but if you use any of .tikz,.TIKZ,.TikZ,.pgf,.PGF, then you don't need to include it in the \includegraphics statement.
Stewart
  • 703