使第二个参数可选

使第二个参数可选

我使用以下resizedtikzpicture环境来重新缩放我的 tikzpictures。是否可以添加一个可选参数,同时考虑 tikz 选项?例如颜色或基线。

梅威瑟:

\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

我的建议是学习如何准备最小工作示例(MWE)也许在遇到错误时阅读错误消息(“它不起作用”不是一个错误消息;它不可操作)。下面的方法确实有效,但使用的方法这里更好,因为它不会缩放字体。

注意:我使用[{#2}]而不是[#2]因为只有前者在#2包含方括号时才能正常工作,这迟早会发生。

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

在此处输入图片描述

使第二个参数可选

如果您想使第二个参数成为可选的,这并不容易\newenvironment,但xparse可以很容易地做到:

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

(与上面相同的输出)。

可选参数不带xparse

没有理由避免xparse这很好,但仅就记录而言,\newenvironment如果您接受将此参数放在第一位,您可以有可选参数:

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

相关内容