1

I have a standalone document that creates a figure using tikz package as below:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1);
\end{tikzpicture}
\end{document}  

I want to put the figure on the very left of my book at a location dictated by [h!]. The book has its paragraphs indented to the left 2.25 inch with respect to the left margin by using adjustwidth command and changepage package. This 2.25 inch indentation is a great place to put the tikz figure. But I do not want to break my paragraph to create a gap because of the figure. Do you know how to do it? Also both figures and captions I like to align all the way to the left.

\documentclass[openany]{book}
\usepackage{tikz}
\usepackage{standalone}
\usepackage{changepage}
\usepackage{geometry}
 \geometry{paperheight=9.8125in,paperwidth=8in, left=.5in, 
right=.5in,top=.75in,bottom=.4375in }
\usepackage{fancyhdr}
\fancypagestyle{plain}{\fancyhf{} \fancyhead[EL,OR]{\textbf 
{\thepage}}\renewcommand{\headrulewidth}{0pt}}
\pagestyle{plain}
\renewcommand{\rmdefault}{pag}
\begin{document}
\begin{adjustwidth}{2.25in}{0pt}
\section{Introduction}\label{Intro}
\chapter {Dogs}
Dogs\\
Dogs.................
\section{Cats}
I put some words before the figure\\
I put some words before the figure\\
\begin{figure}[h!]
 \includestandalone[width=.2\textwidth]{mytikz}%     without .tex extension
 % or use \input{mytikz}
 \caption{My TikZ picture}
 \label{fig:tikz:my}
\end{figure}
 Do not want to break  the words because of the figure. I like to put my 
 figure at the indented margin.\\
 Do not want to break  the words because of the figure. I like to put my 
 figure at the indented margin.
 \end{adjustwidth}
 \end{document}

1 Answer 1

2

I am not sure I understand what exactly you mean.

Do you want to put your figure in the space left free by the indentation of the paragraph's first line? Then something like

\noindent\makebox[\parindent]{\tikz\draw (0,0) -- (1ex,1ex);}

could do the job for you. This first kills the indentation with \noindent to replace it with a \makebox[\parindent] of equivalent width.

image in indentation

Or do you want to have your figure in the margin? Then something like

\leavevmode\marginpar{\flushright\tikz[baseline=-1ex]\draw (0,0) -- (-1,-1);}

may be right. Here, \leavevmode ensures that a new paragraph is started before putting your picture in the \marginpar. You may have to adjust the side and alignment of your margin paragraph using \reversemarginpar and \flushleft or \flushright.

image in margin

Or maybe

\marginpar{\includegraphics{figure}\captionof{figure}{Some figure}}

is what you want. Here, the figure is first compiled into a PDF file named figure.pdf, which is then included as image. The \captionof macro allows to make captions outside floating environments.

image with caption in margin

Here is a complete example:

\documentclass[openany]{book}

\usepackage{lipsum}

\usepackage{tikz}

\usepackage{caption}

\begin{document}

    \lipsum[1]

    \noindent\makebox[\parindent]{\tikz\draw (0,0) -- (1ex,1ex);}\lipsum[2]

    \reversemarginpar
    \leavevmode\marginpar{\flushright\tikz[baseline=-1ex]\draw (0,0) -- (-1,-1);}\lipsum[3]
    \normalmarginpar

    \lipsum[4]\marginpar{\includegraphics{figure}\captionof{figure}{Some figure}}\lipsum[5]

\end{document}
6
  • Thanks DLichti for your answer. I do not want to define tikz file in the body of the main tex and I want to define it externally as a stand alone document. Also to preserve and not to downgrade the figure quality I do not plan to convert it to pdf first. I want to import that mytikz fileto my main tex file and put it in the space left free by the indentation of the paragraph and the location is dictated by [h!] in figure. I tried the first answer but because I defined the special margins and indentation I could not see the figure in my result.
    – Aria
    Commented May 19, 2019 at 13:30
  • @Aria I still don't understand your use of the words 'indentation' and 'margin', so I added some images to make clear, what the different approaches should look like. Btw: TikZ images are stored as vector graphics in PDF, there is no quality loss as in rasterization.
    – DLichti
    Commented May 19, 2019 at 14:16
  • Many thanks DLichti for your follow up. In my main file I defined 0.5 inch margin on left by using geometry. Then for my paragraph I defined 2.25 inch indentation with respect to the margin by using \begin{adjustwidth}{2.25in}{0pt}. So when I say indentation I mean this blank space having 2.25 inch width on the left of the paragraph. The margin 0.5 inch defined by geometry right at the beginning should not use to be occupied by figure.
    – Aria
    Commented May 19, 2019 at 14:21
  • Since I wanna use figure to be labeled and captioned do you know how to use your second answer (leavemode...) by applying "begin {figure}" and "\caption" and "\includestandalone[width=.2\textwidth]{mytikz}" ?
    – Aria
    Commented May 19, 2019 at 14:27
  • I think I once saw a solution for floating environment (e.g. \begin{figure}...\end{figure}) inside \marginpar, but it was hacky and I don't find it anymore. You can still replace \includegraphics by \includestandalone in the third solution.
    – DLichti
    Commented May 19, 2019 at 16:18

You must log in to answer this question.

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