3

I am attempting to create my own wrapper for a tikz image but the output is not as expected. Here is the code

\documentclass{article}

\usepackage{tikz,lipsum, wrapfig}

\newenvironment{wraptikz}[4]{
    \begin{wrapfigure}{#1}{#2cm}
        \vspace{-#3pt}
        \begin{tikzpicture}{scale=#4}
}{
        \end{tikzpicture}
    \end{wrapfigure}
}

\begin{document}
    \begin{wraptikz}[r]{5}{20}{1}
        \draw (0, 0) -- (5, 5);
    \end{wraptikz}
    \lipsum[1]
\end{document}

Which produces the output:

enter image description here

The desired output is:

enter image description here

1

You're defining wraptikz with four mandatory arguments, but are using the syntax for an optional one.

Also, \begin{wrapfigure} should not be used, but rather \wrapfloat{figure} (see Switching between wrapfigure and figure environments).

Here's an implementation that preserves the standard syntax of wrapfigure, with the addition of a trailing mandatory argument for the vertical backspace (however, I'm not sure what it's for) and the final argument has the syntax of an optional argument, so you can pass as many options to tikzfigure as you want, not only scale.

\documentclass{article}

\usepackage{tikz,wrapfig,xparse,lipsum}

\NewDocumentEnvironment{wraptikz}{omO{0pt}mmO{}}{%
  \IfNoValueTF{#1}
    {\wrapfloat{figure}{#2}[#3]{#4}}
    {\wrapfloat{figure}[#1]{#2}[#3]{#4}}
  \vspace{-#5pt}
  \begin{tikzpicture}[#6]
}{%
  \end{tikzpicture}
  \endwrapfloat
}

\begin{document}

\begin{wraptikz}{r}{5cm}{0}[scale=1]
  \draw (0, 0) -- (5, 5);
\end{wraptikz}

\lipsum[1]

\end{document}

enter image description here

  • Hi @egreg, the vspace is used to stop the wrapfigure environment from dropping down by one line on a following paragraph (if another wrapped figure is included). If you have heard of this do you know another way to fix this? I have a post here tex.stackexchange.com/questions/286232/… regarding this problem. – user2850514 Jan 7 '16 at 23:09
1

The first argument of wrapfigure requires curly brackets {}, so it must be {#1}, and the same is true in the new environment. So it becomes \begin{wraptikz}{r}{5}{20}{2}.

The square brackets [] are reserved for optional arguments, but you're not loading any. If you want an optional argument, change the environment definition to

\newenvironment{wraptikz}[4][*]{%
    \begin{wrapfigure}{#1}{#2cm}

But instead of the asterisk, you must enter the default value. For example [r] makes r the default value of the first argument. In this case, you can use square brackets when entering the new environment in the document. Note that wrapfigure still uses the braces in the environment definition.

By the way, wrapfigure requires the wrapfig package.

Output

enter image description here

Code

\documentclass{article}
\usepackage{wrapfig}
\usepackage{tikz}
\usepackage{lipsum}

\newenvironment{wraptikz}[4]{%
    \begin{wrapfigure}{#1}{#2cm}
        \vspace{-#3pt}
        \begin{tikzpicture}[scale=#4]
}{%
        \end{tikzpicture}
    \end{wrapfigure}
}

\begin{document}
\begin{wraptikz}{r}{5}{20}{2}
        \draw (0, 0) -- (5, 0);
\end{wraptikz}
    \lipsum[1]
\end{document}
  • Hi, thanks for the information! It seems the output is still slightly incorrect since it does not seem to be right justifying the image (and therefore wrapping the text around it. – user2850514 Jan 7 '16 at 21:35
  • @user2850514 Well, the point of the question was fix the error in the environment definition. I thought that was your desired output (which did seem odd). – Alenanno Jan 7 '16 at 21:37
  • My apologies! I thought fixing this error would not lead to another error. – user2850514 Jan 7 '16 at 21:38
  • @user2850514 There is no error in the code above. It only doesn't give what you wanted to get. If you could provide a mock-up image of your desired output, it'd be nice. – Alenanno Jan 7 '16 at 21:39
  • I have edited the OP. – user2850514 Jan 7 '16 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.