3

I want to generate a document with graph at left and tikz code at the right. I try to use two minipage but it seems it always layout it as up and down. The minipage is 0.45\textwidth, why latex still can not put it side by side?

The example code as below:

\documentclass[12pt]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{listings}
\usepackage{color}
\usepackage{tikz}
\usepackage{adjustbox}

\lstset{
backgroundcolor=\color{white},
breaklines=true,
language=TeX,
frame=single}

\begin{document}

\begin{minipage}{0.45\textwidth}
  \begin{adjustbox}{max width=0.45\textwidth}
    \begin{tikzpicture}
      \draw (0,0) circle (5cm);
    \end{tikzpicture}
  \end{adjustbox}
\end{minipage}

\begin{minipage}{0.45\textwidth}
  \begin{lstlisting}
    \begin{tikzpicture}
      \draw (0,0) circle (5cm);
    \end{tikzpicture}
  \end{lstlisting}
\end{minipage}

\end{document}

Current output by pdflatex as bleow:

enter image description here

2

You should consider using the showexpl package and use the LTXexample environment.

enter image description here

If you wish to have the code in an external file use the \LTXinputExample environment instead as the second example does in the MWE.

Notes:

  • The filecontents package was used to set up a file to read for this test case. It is not needed in you actual use case.

Code:

\documentclass[12pt]{article}

%\usepackage{filecontents}% <-- Commented to prevent overwrite of test.tex
\begin{filecontents*}{test.tex}
    \begin{tikzpicture}
      \draw [ultra thick, blue] (0,0) circle (2.5cm);
    \end{tikzpicture}
\end{filecontents*}


\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{showexpl}

\lstset{
    backgroundcolor=\color{white},
    breaklines=true,
    language=TeX,
    frame=single,
}

\begin{document}
\begin{LTXexample}[
        width=0.6\linewidth, 
        backgroundcolor=\color{yellow!40},
        basicstyle=\small\ttfamily,
        numbers=none
    ]
    \begin{tikzpicture}
      \draw [ultra thick, blue] (0,0) circle (2.5cm);
    \end{tikzpicture}
\end{LTXexample}

\LTXinputExample[
        width=0.6\linewidth, 
        backgroundcolor=\color{green!40},
        basicstyle=\small\ttfamily,
        numbers=none,
    ]{test}
\end{document}
  • Good suggestion, but if I use \input{test.tikz} replace the tikzpicture, I will get "\input{test.tikz}" at the right, I wish the code at the right but not the command. any idea? – Beatlej Dec 30 '14 at 23:51
  • 1
    @Beatlej: Have updated the example to show how to do that. – Peter Grill Dec 31 '14 at 0:46
  • Thanks, it's really good now. BTW, just now I see pgfplot gallery at pgfplots.sourceforge.net/gallery.html. it really looks nice. do you know which package can get the same behavior? – Beatlej Jan 4 '15 at 2:33
  • @Beatlej: I am not sure I fully understand your question, but below each image is .tex file link that gives the entire .tex file and all the required packages. If that wasn't what you were asking I'd recommend you post a new question. – Peter Grill Jan 5 '15 at 5:42

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.