6

In a memoir class document, I am using the following mdtheorem definition to provide a heading inside an mdframe, which has the effect of providing an increasing count(er) to each new reference to `{discuss}

    \mdtheorem{discuss}{Discuss Topic}[chapter]

Within the body of the document, I've inserted begin{discuss}[\rightmark] (following this page, http://glurl.co/dFD), which has taken me some of the way.

It outputs: Discuss Topic 1: 1 TITLE. I.e., it repeats the discussion (topic/section) number and places the title in CAPS. I would like to only include the counter once AND have the topic title in normal case.

Further to discuss with Gonzalo, it is clear that the following macro, which produces two different types of section (\section and \osection) headings, is effecting the output:

    \usepackage{titlesec}
\usepackage{tikz}\usetikzlibrary{shapes.misc}
\makeatletter
\newcommand\titlebar@{%sections
\tikz[baseline,trim left=3.1cm,trim right=3.0cm] {
    \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=black!100!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.75ex] at (3cm,0.01) {
        \color{white}\textbf{T\thesection}
            };
}}
\newcommand\titlebar@@{%osections
\tikz[baseline,trim left=3.1cm,trim right=3.15cm] {
    \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=black!100!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.75ex] at (3cm,0.15) {
    };
}}
\newcommand\titlebar{\@ifstar\titlebar@@\titlebar@}
\titleformat{\section}{\large\bfseries}{\titlebar}{0.1cm}{}
\renewcommand*{\thesection}{\arabic{section}}
\newcommand{\osection}[1]{\section*{\titlebar*#1}}
\makeatother
johnbrc
  • 575

1 Answers1

5

You could use memoir's \currenttitle:

\documentclass{memoir}
\usepackage{mdframed}

\mdtheorem[style=mpdframe]{discuss}{Discuss Topic}[chapter]

\begin{document}

\chapter{Test chapter} \section{Test section} \label{sec:test} \begin{discuss}[\currenttitle] test text \end{discuss}

\end{document}

enter image description here

The problem with the above code is that \currenttitle stores the last sectional unit used, which might not be a section; in this case, some additional work will do; a patch to \M@sect was used to store the name of the current section in \currentsectionname:

\documentclass{memoir}
\usepackage{mdframed}

\mdtheorem[style=mpdframe]{discuss}{Discuss Topic}[chapter] \makeatletter \let\currentsectiontitle\relax \patchcmd{\M@sect} {\ifheadnameref} {\ifnum#2=1\relax \gdef\currentsectiontitle{#9} \fi \ifheadnameref} {} {} \makeatother

\begin{document}

\chapter{Test chapter} \section{Test section} \label{sec:test} \begin{discuss}[\currentsectiontitle] test text \end{discuss}

\end{document}

The definition for the mpdframe style was missing in the question.

As mentioned in a comment to the question, since this is going to be used for every discuss environment, I think it's better to use \newmdtheoremenv with a custom theorem style to automatically provide the current section title; something along these lines:

\documentclass{memoir}
\usepackage{amsthm}
\usepackage{mdframed}

\newtheoremstyle{mystyle} {\topsep} {\topsep} {} {} {\bfseries} {\newline} {.5em} {\thmname{#1}~\thmnumber{#2}: \currentsectiontitle} \theoremstyle{mystyle} \newmdtheoremenv{discuss}{Discuss Topic}[chapter]

\makeatletter \let\currentsectiontitle\relax \patchcmd{\M@sect} {\ifheadnameref} {\ifnum#2=1\relax \gdef\currentsectiontitle{#9} \fi \ifheadnameref} {} {} \makeatother

\begin{document}

\chapter{Test chapter} \section{Test section one} \subsection{Test subsection} \label{sec:test} \begin{discuss} test text \end{discuss} \section{Test section two} \subsection{Test subsection} \label{sec:test} \begin{discuss} test text \end{discuss}

\end{document}

enter image description here

Update after the edit to the question

The above suggestions won't work since the titlesec package is used (which might be not a good idea with the memoir class; see About memoir and titlesec incompatibility). In this case you can use:

\documentclass{memoir}
\usepackage{mdframed}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\mdtheorem[style=mpdframe]{discuss}{Discuss Topic}[chapter]

\makeatletter

\let\currentsectiontitle\relax

\newcommand\titlebar@{%sections \tikz[baseline,trim left=3.1cm,trim right=3.0cm] { \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex); \node [ fill=black!100!white, anchor= base east, rounded rectangle, minimum height=3.75ex] at (3cm,0.01) { \color{white}\textbf{T\thesection} }; }} \newcommand\titlebar@@{%osections \tikz[baseline,trim left=3.1cm,trim right=3.15cm] { \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex); \node [ fill=black!100!white, anchor= base east, rounded rectangle, minimum height=3.75ex] at (3cm,0.15) { }; }} \newcommand\titlebar{@ifstar\titlebar@@\titlebar@} \titleformat{\section} {\large\bfseries} {\titlebar} {0.1cm} {\gdef\currentsectiontitle{#1}#1} \renewcommand{\thesection}{\arabic{section}} \newcommand{\osection}[1]{\section{\titlebar*#1}} \makeatother

\begin{document}

\chapter{Test chapter} \section{Test section} \label{sec:test} \begin{discuss}[\currentsectiontitle] test text \end{discuss}

\end{document}

enter image description here

Or with the automatic inclussion via a new theorem style:

\documentclass{memoir}
\usepackage{amsthm}
\usepackage{mdframed}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\newtheoremstyle{mystyle} {\topsep} {\topsep} {} {} {\bfseries} {\newline} {.5em} {\thmname{#1}~\thmnumber{#2}: \currentsectiontitle} \theoremstyle{mystyle} \newmdtheoremenv{discuss}{Discuss Topic}[chapter]

\makeatletter

\let\currentsectiontitle\relax

\newcommand\titlebar@{%sections \tikz[baseline,trim left=3.1cm,trim right=3.0cm] { \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex); \node [ fill=black!100!white, anchor= base east, rounded rectangle, minimum height=3.75ex] at (3cm,0.01) { \color{white}\textbf{T\thesection} }; }} \newcommand\titlebar@@{%osections \tikz[baseline,trim left=3.1cm,trim right=3.15cm] { \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex); \node [ fill=black!100!white, anchor= base east, rounded rectangle, minimum height=3.75ex] at (3cm,0.15) { }; }} \newcommand\titlebar{@ifstar\titlebar@@\titlebar@} \titleformat{\section} {\large\bfseries} {\titlebar} {0.1cm} {\gdef\currentsectiontitle{#1}#1} \renewcommand{\thesection}{\arabic{section}} \newcommand{\osection}[1]{\section{\titlebar*#1}} \makeatother

\begin{document}

\chapter{Test chapter} \section{Test section one} \subsection{Test subsection} \begin{discuss} test text \end{discuss} \section{Test section two} \subsection{Test subsection} \begin{discuss} test text \end{discuss}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • This goes a long way. It's actually the \thesection title I'm wanting to insert. This inserts \thechapter title… – johnbrc Apr 08 '14 at 14:47
  • @johnbrc did you see the title of your question? You asked to include the chapter name. Please correct the title. – Gonzalo Medina Apr 08 '14 at 14:48
  • @johnbrc please see my updated answer. Is it something like that what you need? – Gonzalo Medina Apr 08 '14 at 14:51
  • Thanks, @Gonzalo, I'm not sure this is what I'm looking for. It works in the example, because the label is defined. My section label is being defined automatically each time, because I'm writing in MultiMarkdown and exporting into LaTex, before compiling to pdf. I don't want to have to manually insert a label to each section. I liked your previous solution, only for the section, rather than chapter. Can it be adapted? – johnbrc Apr 08 '14 at 15:10
  • @johnbrc please see my updated answer. – Gonzalo Medina Apr 08 '14 at 15:21
  • Right. Your example works in freestanding. In the context of my document, using \currenttitle inserts the Chapter title, not the section title, as required. – johnbrc Apr 08 '14 at 15:25
  • @johnbrc my example shows otherwise, can you provide a MWE showing the effect you mention? – Gonzalo Medina Apr 08 '14 at 15:26
  • My document is long and relatively complex, I'm not sure how to do this readily. But if I manage, I'll post it. – johnbrc Apr 08 '14 at 15:30
  • @johnbrc no need for that. In the meantime I've updated my answer providing a solution that should give you the desired result. – Gonzalo Medina Apr 08 '14 at 15:41
  • @johnbrc in fact, taking into account the answer to my comment in your question, I've provided a better option producing the section title automatically for every discuss environment. – Gonzalo Medina Apr 08 '14 at 15:53
  • thanks for your attention to this. I've inserted your solution into my preamble. It compiles to LaTex, from MMD, with \begin{discuss}[\currentsectiontitle], so no problem there. However, the pdf output is a blank… – johnbrc Apr 08 '14 at 16:03
  • @johnbrc I have no idea what "MMD" is. My solution will work using an updated LaTeX system. If it is not working for you, then, as I said before, build a MWE (just a simple complete document as the ones I used in my answer) illustrating your problem and add it as an edit to your question. – Gonzalo Medina Apr 08 '14 at 16:08
  • Sorry for the confusion, it's me thinking out loud. It's not relevant to the problem. For some reason, begin{discuss}[\currentsectiontitle] is outputting blank, that's all I should have said. – johnbrc Apr 08 '14 at 16:13
  • @johnbrc Ah, OK; no problem. As I said before, in order to diagnose where the problem might be, please compose a MWE reproducing the undesired behaviour and add it as an edit to your question. – Gonzalo Medina Apr 08 '14 at 16:15
  • understood! working on it now… – johnbrc Apr 08 '14 at 16:16
  • in producing a MWE, I've discovered that another \input file is causing the blank output. Should I edit my original question to include that tex or start another question? (It's 30-odd lines so too much for a comment). Regards. – johnbrc Apr 08 '14 at 16:48
  • I see you've already instructed me on this. I've edited my original question to include the other macro / tex that is effecting the output. – johnbrc Apr 08 '14 at 16:56
  • @johnbrc please see my updated answer. Notice that now the explicit option for titlesec is used, and that you're \titleformat for sections was slightly modified. – Gonzalo Medina Apr 08 '14 at 17:01
  • Wow. Thank you. As well as being blown away by the technical expertise you've shown to assist me, I'm even more blown away by the grace that is in evidence in your gift. Again. Thank you. – johnbrc Apr 08 '14 at 17:14
  • @johnbrc You're welcome. Thank you for your kind words! – Gonzalo Medina Apr 08 '14 at 17:19
  • "Open source" thinking and contribution repeatedly impresses me. No reason why it should be of any interest, but f/w/i/worth this is the (non-commercial) educational project you've contributed towards in helping me today: http://glurl.co/dFJ. – johnbrc Apr 08 '14 at 17:33