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

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}}{}
  • Usually this means you need to \leavevmode. – John Kormylo Aug 5 '17 at 13:25
  • @JohnKormylo, yes that might be even better. I have updated – StefanH Aug 5 '17 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 Aug 5 '17 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 Aug 6 '17 at 13:21

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.