1

I use the following resizedtikzpicture environment in order to rescale my tikzpictures. Would it be possible to add an optional argument, taking into account the tikz option? For example the color or baseline.

MWE:

\documentclass[twocolumn]{article}

\usepackage{tikz,tikzpagenodes}

\newsavebox\mybox
\newenvironment{resizedtikzpicture}[1]{%
  \def\mywidth{#1}%
  \begin{lrbox}{\mybox}%
  \begin{tikzpicture}
  }{%
  \end{tikzpicture}%
  \end{lrbox}%
  \resizebox{\mywidth}{!}{\usebox\mybox}%
}

%\newsavebox\mybox
%\newenvironment{wrongresizedtikzpicture}[2][\unskip]{%
%  \def\mywidth{#1}%
%  \begin{lrbox}{\mybox}%
%  \begin{tikzpicture}[ #2]
%  }{%
%  \end{tikzpicture}%
%  \end{lrbox}%
%  \resizebox{\mywidth}{!}{\usebox\mybox}%
%}


\begin{document}
ABCDE

\begin{resizedtikzpicture}{\columnwidth}
\draw [thick,->] (-5,0)--(5,0);
\draw [thick,->] (0,-5)--(0,5);
\draw [very thick, domain=-5:5,samples=200] plot(\x,{\x+0.2});

\draw [very thick, domain=-5:0,samples=200] plot(\x,{\x});
\draw [very thick, domain=0:5,samples=200] plot(\x,{0*\x});

\draw [very thick, domain=-5:0,samples=200] plot(\x,{0*\x});
\draw [very thick, domain=0:5,samples=200] plot(\x,{\x});

\draw (-6,-4.5) node{{\(x \mapsto x\)}};
\end{resizedtikzpicture}

% Goal :
%\begin{resizedtikzpicture}{\columnwidth}[baseline=0]
%\draw [thick,->] (-5,0)--(5,0);
%\draw [thick,->] (0,-5)--(0,5);
%\draw [very thick, domain=-5:5,samples=200] plot(\x,{\x+0.2});
%
%\draw [very thick, domain=-5:0,samples=200] plot(\x,{\x});
%\draw [very thick, domain=0:5,samples=200] plot(\x,{0*\x});
%
%\draw [very thick, domain=-5:0,samples=200] plot(\x,{0*\x});
%\draw [very thick, domain=0:5,samples=200] plot(\x,{\x});
%
%\draw (-6,-4.5) node{{\(x \mapsto x\)}};
%\end{resizedtikzpicture}

\end{document}

1 Answer 1

3

My advice would be to learn how to prepare a minimal working example (MWE) and maybe to read the error message when you encounter an error (“it doesn't work” is not an error message; it is not actionable). The following does work, but the method used here is better, as it wouldn't scale fonts.

Note: I used [{#2}] instead of [#2] because only the former works fine if #2 contains square brackets, which is bound to happen sooner or later.

\documentclass[twocolumn]{article}
\usepackage{tikz}
\usepackage{lipsum}

\newsavebox\mybox
\newenvironment{resizedtikzpicture}[2]{%
  \def\mywidth{#1}%
  \begin{lrbox}{\mybox}%
  \begin{tikzpicture}[{#2}]
  }{%
  \end{tikzpicture}%
  \end{lrbox}%
  \resizebox{\mywidth}{!}{\usebox\mybox}%
}

\begin{document}

\twocolumn[{\lipsum[1]}]

\noindent
\makebox[0pt][l]{abcd.}%
\begin{resizedtikzpicture}{\columnwidth}{baseline=0}
  \draw [thick,->] (-5,0)--(5,0);
  \draw [thick,->] (0,-5)--(0,5);
  \draw [very thick, domain=-5:5,samples=200] plot(\x,{\x+0.2});

  \draw [very thick, domain=-5:0,samples=200] plot(\x,{\x});
  \draw [very thick, domain=0:5,samples=200] plot(\x,{0*\x});

  \draw [very thick, domain=-5:0,samples=200] plot(\x,{0*\x});
  \draw [very thick, domain=0:5,samples=200] plot(\x,{\x});

  \draw (-6,-4.5) node{{\(x \mapsto x\)}};
\end{resizedtikzpicture}

\end{document}

enter image description here

Making the second argument optional

If you want to make the second argument optional, this is not straightforward with \newenvironment, but xparse makes it easy:

\documentclass[twocolumn]{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{lipsum}

\newsavebox\mybox
\NewDocumentEnvironment{resizedtikzpicture}{mO{}}{%
  \def\mywidth{#1}%
  \begin{lrbox}{\mybox}%
  \begin{tikzpicture}[{#2}]
  }{%
  \end{tikzpicture}%
  \end{lrbox}%
  \resizebox{\mywidth}{!}{\usebox\mybox}%
}

\begin{document}

\twocolumn[{\lipsum[1]}]

\noindent
\makebox[0pt][l]{abcd.}%
\begin{resizedtikzpicture}{\columnwidth}[baseline=0]
  \draw [thick,->] (-5,0)--(5,0);
  \draw [thick,->] (0,-5)--(0,5);
  \draw [very thick, domain=-5:5,samples=200] plot(\x,{\x+0.2});

  \draw [very thick, domain=-5:0,samples=200] plot(\x,{\x});
  \draw [very thick, domain=0:5,samples=200] plot(\x,{0*\x});

  \draw [very thick, domain=-5:0,samples=200] plot(\x,{0*\x});
  \draw [very thick, domain=0:5,samples=200] plot(\x,{\x});

  \draw (-6,-4.5) node{{\(x \mapsto x\)}};
\end{resizedtikzpicture}

\end{document}

(same output as above).

Optional argument without xparse

There is no reason to avoid xparse which is great, but just for the record, you could have your optional argument with \newenvironment if you accept to have this argument in first position:

\newenvironment{resizedtikzpicture}[2][]{%
  \def\mywidth{#2}%
  \begin{lrbox}{\mybox}%
  \begin{tikzpicture}[{#1}]
  }{%
  \end{tikzpicture}%
  \end{lrbox}%
  \resizebox{\mywidth}{!}{\usebox\mybox}%
}

...

\begin{resizedtikzpicture}[baseline=0]{\columnwidth}
2
  • Thank's, but with this solution the second argument is not optional (that's my problem).
    – jowe_19
    Apr 12, 2020 at 23:38
  • 1
    Ah, I had not understood that from your question, since you wrote “Goal : \begin{resizedtikzpicture}{\columnwidth}{baseline=0}”, which is a syntax for a mandatory argument. I have added another example where the second argument is optional (thus, you were right tagging the question with xparse and I'll readd it).
    – frougon
    Apr 13, 2020 at 0:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .