1

Using ocgx2 and pdflatex I would like to switch between two tikzpictures that are identical in size. If I do

\switchocg{ocg23a ocg23b}{Lösung}

\begin{ocg}{OCG 1}{ocg1}{1}
  \begin{tikzpicture}
      \draw[draw=black] (0, 0) rectangle (2, 2);
  \end{tikzpicture}
\end{ocg}

\begin{ocg}{OCG 2}{ocg2}{0}
  \begin{tikzpicture}
      \draw[draw=blue] (0, 0) rectangle (2, 2);
  \end{tikzpicture}

it does switch between the images. However the images being replaced are not at the same position on the page but one above the other.

How can I achieve, that the images are placed at the same position on the page?

1
  • Please make your example complete and compilable.
    – AlexG
    Jun 10 at 13:23
3

The ocg environment does not prevent its content from taking up space in the output and to push the following content (the second tikzpicture) forward. Thus, you have to put the first tikzpicture in a zero-width, left aligned LR box.

Also, unprotected line endings and empty lines produce space and paragraphs, respectively, in the output.

The fixed code might look like this (the ocg references in the switch command had to be corrected too):

\documentclass{article}
\usepackage{ocgx2}
\usepackage{tikz}

\begin{document}
\switchocg{ocg1,ocg2}{Lösung}

\begin{ocg}{OCG 1}{ocg1}{on}
  \makebox[0pt][l]{\begin{tikzpicture} %<== make tikzpicture zero-width
      \draw[draw=black] (0, 0) rectangle (2, 2);
  \end{tikzpicture}}
\end{ocg}%                              <== protected line ending
%                                       <== commented paragraph
\begin{ocg}{OCG 2}{ocg2}{off}
  \begin{tikzpicture}
      \draw[draw=blue] (0, 0) rectangle (2, 2);
  \end{tikzpicture}
\end{ocg}
\end{document}
7
  • seem it should be \switchocg{ocg1 ocg2}{Lösung}?
    – lyl
    Jun 17 at 1:52
  • With ocgx2 both, space and comma, can be used as separators. Comma was added to the package as it looks more conform to LaTeX standards.
    – AlexG
    Jun 17 at 5:46
  • Many thanks! And is there any way to make a picture(e.g. by \includegraphics{...}) as the sencond arguments of \switchocg instead of general text?
    – lyl
    Jun 17 at 12:16
  • Yes, anything that can be typeset works as button face.
    – AlexG
    Jun 17 at 12:54
  • I tried this: \switchocg{bla}{\includegraphics{mypicture}}, the picture was typeset, but when I click it, the matter bla point to did not hide/show. Contrary \switchocg{bla}{button} works.
    – lyl
    Jun 17 at 13:05

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.