5

I am not facing any problem, but I need the opinion of the community.

I am writing a thesis and have a lot of tikz figures in it. So what I do is pre-compile these tikz files to PDF and then include them in the thesis. This makes the re-compiling of the thesis faster as no longer I have to compile the tikz files again and again, when I do minor modification in the text.

I have written a macro to include graphics like this -

\newcommand\myIncludeGraphics[2]{
\ifdefined\embedTikzGraphics
    \scalebox{#1}{\input{#2.tikz}}
\else
    \includegraphics[scale=#1]{#2}
\fi
}

and I include the graphics like this -

\begin{figure}
    \myIncludeGraphics{0.9}{chapters/chap1/Figures/fig1}
\end{figure}

Here 0.9 specifies how much to scale the figure. However, sometimes I would like to place two figures side-by-side and then I need to manually calculate the scaling factor.

\subfloat[CAP1]{\label{fig:lab1a}{\resizebox{0.5\columnwidth}{!}{\includegraphics{chapters/chap1/Figures/fig1a}}}}
\subfloat[CAP2]{\label{fig:lab1b}{\resizebox{0.5\columnwidth}{!}{\includegraphics{chapters/chap1/Figures/fig1b}}}}

This will help me to fit the two figure exactly to column-width of the document. However, I lost the capability of embedded tikz files.

I want some suggestions if I can cover all three scenarios in one macro keeping the code clean. I should be able to use scaling factor or width=\columnwidth.

If this is not possible, it is fine. Please excuse, if I didn't define the problem correctly.

Thank You

UPDATE

Just to summarize what I am trying to achieve is the following.

  • I should be able to use the tikz picture integrated in the main pdf or tikz picture which is compile separately and then included in pdf using includegraphics.
  • When using includegraphics, I should be able to specify either width of the figure or scale factor of the figure.
  • When using the tikz picture integrated in main pdf, I will not change the macro in my tex. For example the macro usage will look like myIncludeGraphics{scalefactor=0.9}{chapters/chap1/Figures/fig1} or myIncludeGraphics{figWidth=0.5\columnwidth}{chapters/chap1/Figures/fig1}. Depending on the first argument, the macro should use scalebox or resizebox. This is because scalebox does not understand 0.5\columnwidth and the other way round for the resizebox.
7
  • 2
    You know the externalization library of TikZ (\usetikzlibrary{external}\tikzexternalize)? See the TikZ manual, section "50 Externalization Library". Jun 19, 2014 at 18:47
  • I use a makefile rule to take the tikz code and a template to generate a PDF out of it. The advantage of it is I can issue a parallel execution of make command so that it takes lesser time than using externalize. Jun 20, 2014 at 8:32
  • @Manuel scaling is not really my problem. My problem is to be able to mention in the same macro either scalefactor or width of the image. Jun 20, 2014 at 8:40
  • @Raj The problem is (if I understood correctly) that the font size, for instance won't be of the same size. That means, the only way to output correct images with tikz is scale and if you want to use, e.g., width, you need one of those “tricks” I linked. And, finally, if you want to be able to call both of them, it's just a little hack to tell LaTeX wether to use scale or those systems to resize to a fixed length.
    – Manuel
    Jun 20, 2014 at 8:43

2 Answers 2

4

A possible solution to maintain the macro's interface clean is to use a key-val parameter using pgfkeys:

\documentclass{article}
\usepackage{tikz}


\newcommand\myIncludeGraphics[2][]{
\ifdefined\embedTikzGraphics
    \pgfkeys{
        my include/.cd,
        scale/.store in=\myScaleFactor,
        scale=1,
        width/.initial=0,
        % define here similarly the graphicx keys you want to support
        #1
    }
    \scalebox{\myScaleFactor}{\input{#2.tikz}}
\else
    \includegraphics[#1]{#2}
\fi
}

\begin{document}

\myIncludeGraphics{tikzdrawing}
\myIncludeGraphics[scale=0.1]{tikzdrawing}
\myIncludeGraphics[width=.5\columnwidth]{tikzdrawing}

\end{document}

Here you can pass to the macro any argument accepted by graphix but when \embedTikzGraphics is defined you will parse them with pgfkeys, extract the scale factor and use that directly.

Update: Another option is using adjustbox:

\usepackage{adjustbox}

\newcommand\myIncludeGraphics[2][]{
\ifdefined\embedTikzGraphics
    \begin{adjustbox}{#1}
        \input{#2.tikz}
    \end{adjustbox}
\else
    \includegraphics[#1]{#2}
\fi
}
4
  • this is very close to what I was looking for. Just have one doubt about your answer. Suppose if I want to make scale or width a compulsory argument of the macro, is it possible? Also, I wanted to use resizebox when I specify width of the figure and scalebox (as you showed in your answer) when I specify the scaling factor. Jun 20, 2014 at 8:47
  • absolutely! First, the key-val arg does not need to be an optional argument; however there are sensible defaults here so it makes sense to make it optional. Second, you may enforce that at least one is passed by checking that with ifs...but I do not see the usefulness of that...
    – Bordaigorl
    Jun 20, 2014 at 8:50
  • 1
    Added another option: adjustbox accepts the same arguments as \includegraphics
    – Bordaigorl
    Jun 20, 2014 at 8:57
  • this is perfectly what I was looking for! thanks a lot. Jun 20, 2014 at 9:00
3

As I already commented, scaling doesn't take in account the widths, sizes, etc. in the TikZ code. It just scales the result. With tikzscale you can scale only the coordinates, and have everything draw as it was intended in first place (that's the same that [scale = X] does inside the options of tikzpicture).

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{tikzscale}

\begin{filecontents}{fig.tikz}
    \begin{tikzpicture}[y=.1cm] %
        \draw[ultra thick] (0,0) circle (1);
        \draw[red] (0,0) node {$A$};
        \draw[color=blue] (0,0) rectangle (1,1);
    \end{tikzpicture}
\end{filecontents}


\begin{document}

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
\begin{center}
    \resizebox{.5\textwidth}{!}{\input{fig.tikz}}%
    \includegraphics[width=.5\linewidth]{fig.tikz}
\end{center}
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

\end{document}

gives you

enter image description here

where I clearly prefer the right one.

So the solution I propose is, use tikzscale with \includegraphics and only use width or height. Of course, one could add the scale option, but since the figure is already written on a file, it would be the same as the image on the left, which I think is not correct.

2
  • I now clearly understand your point. thanks for the explanation. In your opinion, one should not use resizebox for correct results? Thanks ! Jun 20, 2014 at 9:34
  • That's it. That's the same that scale does in the options of TikZ. For instance, compare \scalebox{5}{\input{fig.tikz}} with the same tikzpicture but add scale=5: \begin{tikzpicture}[scale=5]…\end{tikzpicture}.
    – Manuel
    Jun 20, 2014 at 9:37

You must log in to answer this question.

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