3

Well consider the following code:

\documentclass[a4, oneside,openleft]{book}
\usepackage{listings}
\begin{document}
\begin{flushright}\begin{minipage}{0.9\textwidth}\begin{lstlisting}
for (i) {
    hello world i;
}
\end{lstlisting}\end{minipage}\end{flushright}
\end{document}

Now I wish to have this as a single environment "codebox". So I tried (the simplified):

\documentclass[a4, oneside,openleft]{book}
\usepackage{listings}
\lstnewenvironment{codebox}{\begin{flushright}}{\end{flushright}}

\begin{document}
\begin{codebox}
for (i) {
    hello world i;
}
\end{codebox}
\end{document}

However this returns the following error:

Overfull \hbox (3190.24414pt too wide) in paragraph at lines 59--61 [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][ ][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][ ][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][ ][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] )

So how to fix this?

  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jub0bs Apr 15 '15 at 8:45
  • @Jubobs The question is NOT answered. I find it quite arrogant to see such a comment, assuming I don't know what to do. Especially as I said in the comments to the answerer that the solution doesn't provide me with everything I need. – paul23 Apr 16 '15 at 1:15
  • For information, my earlier comment is one of the stock comments listed on the meta site; it wasn't meant to be arrogant, but simply to encourage you to accept the answer you got, if it solved your problem. I thought it did; my mistake. – jub0bs Apr 16 '15 at 8:13
5

listings way of defining new code environments is

\lstnewenvironment{codebox}
   {\lstset{language = C,linewidth=0.9\linewidth}}
   {}

Where you specify your language. To flush right as in your first code, you can use xleftmargin in \lstset

\documentclass[a4, oneside,openleft]{book}
\usepackage{showframe}  %% just for demo
\usepackage{listings}
\lstnewenvironment{codebox}
  {\lstset{language = C,linewidth=0.9\linewidth,xleftmargin=0.1\linewidth}}
  {}

\begin{document}
\begin{codebox}
for (i) {
    hello world i;
}
\end{codebox}

\end{document}

enter image description here

  • I wish to do some formatting (ie flushright) that can't be done through lstset... – paul23 Mar 11 '15 at 1:03
  • @paul23 As in your first code? You can use xleftmargin. See the update. – user11232 Mar 11 '15 at 1:18
  • that indeed works for this case - but for the generic case where I wish to create a combined environment with minipage etc? – paul23 Mar 11 '15 at 10:28
  • @paul23 Why do you want a minipage inside a listing? – user11232 Mar 11 '15 at 10:58
  • 1
    @paul23 Could you elaborate on how Harish's answer isn't satisfactory? That would help others find a solution to your problem. – jub0bs Apr 16 '15 at 8:37
0

The problem with your second snippet isn't really the overfull box (which is a warning, not an error), but the fact that using a flushright environment in

\lstnewenvironment{codebox}
  {\begin{flushright}}
  {\end{flushright}}

causes LaTeX to raise a (job aborted, no legal \end found) error. You could remedy that error by using a \raggedleft instead of a flushright environment, like so:

\lstnewenvironment{codebox}
  {\raggedleft}
  {}

but that still wouldn't affect the vertical alignment of your listing. For a workaround, see below.

By the way, a4 and openleft are not valid options for book. Did you mean a4paper and openany, instead?

enter image description here

\documentclass[a4paper, oneside, openany]{book}

\usepackage{listings}
\usepackage{lipsum}

\lstnewenvironment{codebox}{%
  \lstset{frame=single}%
  \vspace{\baselineskip}
  \hfill%
  \minipage{.9\textwidth}%
}{%
  \endminipage%
}

\begin{document}

\begin{codebox}
for (i) {
    hello world i;
}
\end{codebox}

\lipsum[2]

\end{document}
  • Again, correct me if I understood it wrongly: but this puts the a minipage INSIDE the codebox (which would then fill the whole page width). I am afraid this would lead to problems when say I wish to add a background to the codebox. – paul23 Apr 16 '15 at 11:56
  • @paul23 This approach puts your listing in a minipage. I don't anticipate any problem related to background colour. Try it for yourself and see. – jub0bs Apr 16 '15 at 12:11

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.