2

I want to write HTML codes of two pages side by side in LaTeX like as follows:

<html>   <html>
<head>   <head>
</head>  </head>
<body>   <body>
 ...      ...
</body>  </body>
</html>  </html>

I know how to include HTML code in LaTeX but I have no clue how will I achieve it. So if anyone please help me regarding this I will be grateful. Thank you.

EDIT: I have tried it with the help of table as shown below, but it is giving errors.

\begin{table}[htb]
\parbox{.45\linewidth}{
 \centering
 \begin{tabular}{c}

 \begin{lstlisting}[language=html]
 <html>
 <head>
 </head>
 <body>
  ...
 </body>
</html>
 \end{lstlisting}
\end{tabular}   
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{c}

\begin{lstlisting}[language=html]
<html>
<head>
</head>
<body>
...
</body>
</html>
\end{lstlisting}
\end{tabular}   
}
\end{table}
Joy
  • 287
  • So what you actually want is to put two blocks of HTML code side by side, so it can be compared and stuff? could you post what you have so far? – Count Zero Jun 10 '13 at 09:58
  • Yes Sir you are right. I exactly I want that. – Joy Jun 10 '13 at 10:03

2 Answers2

2

How about using minipage environments for the two columns?

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{listings}
\lstset{
breaklines=true,
breakatwhitespace=true,
}
\begin{document}

\begin{minipage}[t]{0.45\textwidth}
\begin{lstlisting}[language=html]
<html>
<head>
</head>
<body>
...
</body>
</html>
 \end{lstlisting}
\end{minipage}
\hfill
\begin{minipage}[t]{0.45\textwidth}
\begin{lstlisting}[language=html]
<html>
<head>
</head>
<body>
...
</body>
</html>
\end{lstlisting}
\end{minipage}   

\end{document}
jub0bs
  • 58,916
  • Thanks a lot, Sir :) It's working. But I could not include the package geometry with showframe option. Without that it is working. – Joy Jun 10 '13 at 10:15
  • Sir one problem is occurring, that sometimes lines from left html code get intermixed with that of right. How will I prevent that? – Joy Jun 10 '13 at 10:20
  • @Joy: use line breaks in your code. – Willie Wong Jun 10 '13 at 12:28
  • @Joy Call me Jubobs, not "Sir" :) If you want to allow line breaks in your code, you should set breaklines=true; see my last edit. – jub0bs Jun 10 '13 at 13:25
1

Use package varwidth for a variable width of the parboxes:

\documentclass{article}
\usepackage{listings}
\usepackage{varwidth}
\begin{document}
\noindent
\begin{varwidth}{0.49\linewidth}
\begin{lstlisting}[language=html]
 <html>
 <head>
 </head>
 <body>
  ...
 </body>
</html>
\end{lstlisting}
\end{varwidth}
\qquad%\hfill
\begin{varwidth}{0.49\linewidth}
\begin{lstlisting}[language=html]
<html>
<head>
</head>
<body>
...
</body>
</html>
\end{lstlisting}
\end{varwidth}

\end{document}

enter image description here

  • But sometimes lines from left html code get intermixed with that of right, if that line from left is bit long. How will I prevent that? – Joy Jun 10 '13 at 10:22
  • then use \begin{varwidth}{\linewidth} instead, then they can be each as wide as the linewidth. If one is wider than that use a bigger value or use something like basicstyle=\footnotesize\ttfamily for the listings setting. –  Jun 10 '13 at 10:24
  • Sorry sir, I do not understand the last part of your answer i.e. basicstyle for listings setting. Could you please clarify? – Joy Jun 10 '13 at 10:28
  • \begin{lstlisting}[language=html,basicstyle=\footnotesize\ttfamily] –  Jun 10 '13 at 10:37
  • Sorry Sir, even after using that lstlisting setting problem is not solved. – Joy Jun 10 '13 at 10:43
  • then show an example with that "problem"! –  Jun 10 '13 at 10:56
  • First of all varwidth is not working,as it is telling varwidth.sty is missing, so I am using minipage. With that I have used the setting as you told. But still now the texts of these two blocks get intermixed and if any of line in the right part is bit long it is going out of the page. This is "problem"! – Joy Jun 10 '13 at 11:00
  • @Joy you can install the missing varwidth (and geometry if you want, but that was mainly for illustration) easily from an online source. How exactly you do that depends on OS / TeX distribution, so if you provide those I am sure someone will point you to the How-To. – ach Jun 10 '13 at 11:23