1

I am typesetting answers to textbook exercises. I use a ntheorem theorem named questionas wrapper for the questions. Some of the answers begin with regular text, and I want them to start on the same line as the incipit Question number XX.

To achieve this, I defined

\usepackage{ntheorem}
\theoremstyle{plain}
\newtheorem{question}{Question}

However, some answers only consist in a piece of code that I typeset with a custom lstlisting environment

\usepackage{listings}
\lstnewenvironment{lstcaml}[1][]{\lstset{language=[Objective]caml, frame=single, frameround=tttt, #1}}{}

within the question environment. In this case I want the listing to start on the next line, for code indentation reasons and to get an unbroken frame around it.

It does not work as I expected :

Result of MWE

is obtained from

\begin{document}
  \begin{question}
     Some text
     \begin{lstcaml}
        Some code
     \end{lstcaml}
   \end{question}

  \begin{question}

    \begin{lstcaml}
       Some code
    \end{lstcaml}
  \end{question}
  %% the same follows without borders for listings

I tried to add a \par in the definition of my listings environnment, to no avail (I had to cheat with \let\mypar\par because these tokens seem to be used in a non-\long definition).

Manually typing \ \\ at the beginning works but it is cumbersome and I think it takes more vertical space than needed.

1 Answer 1

1

It is enough to have something, like a letter or \strut or \leavevmode, after the theorem title:

\begin{question}\leavevmode
  \begin{lstcaml}
    Some code
  \end{lstcaml}
\end{question}

To do this for all instances you can define your own theorem style. Here I have taken the theorem style for plain and added a \strut outside the theorem title (\theoremseparator turns up inside the title and cannot be used for this). A total MWE becomes:

\documentclass{article}
\usepackage{ntheorem}
\makeatletter
\newtheoremstyle{myplain}%
  {\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\theorem@separator]\leavevmode}%
  {\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\ (##3)\theorem@separator]}
\makeatother
\theoremstyle{myplain}
\newtheorem{question}{Question}
\usepackage{listings}
\lstnewenvironment{lstcaml}[1][]{\lstset{language=[Objective]caml, frame=single, frameround=tttt, #1}}{}
\begin{document}
\begin{question}
  Some text
  \begin{lstcaml}
    Some code
  \end{lstcaml}
\end{question}

\begin{question}
  \begin{lstcaml}
    Some code
  \end{lstcaml}
\end{question}
\end{document}

enter image description here

EDIT

As pointed out by ysalmon, the same can achieved by seting the \leavevmode in the lstcaml definition. Then the preamble becomes:

\usepackage{ntheorem}
\theoremstyle{plain}
\newtheorem{question}{Question}
\usepackage{listings}
\lstnewenvironment{lstcaml}[1][]{\leavevmode\lstset{language=[Objective]caml, frame=single, frameround=tttt, #1}}{}
4
  • Usually this means you need to \leavevmode. Commented Aug 5, 2017 at 13:25
  • @JohnKormylo, yes that might be even better. I have updated
    – StefanH
    Commented Aug 5, 2017 at 13:27
  • Thanks a lot. In fact, defining \lstnewenvironment{lstcaml}[1][]{\leavevmode\lstset{language=[Objective]caml, frame=single, frameround=tttt, #1}}{} works too and seems a little simpler.
    – ysalmon
    Commented Aug 5, 2017 at 13:38
  • @ysalmon, Even better. It should give the same result and you don't have to define your own plain. I have added it to the answer.
    – StefanH
    Commented Aug 6, 2017 at 13:21

You must log in to answer this question.

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