8

I want to be able to insert Markdown text in a LaTeX document with a simple macro, and I want that text to always have a specific background color (e.g. light gray).

I tried using the markdown package some of the solutions listed below without luck:

I think the challenge that I am facing is that markdown is itself a block.

Here's a MWE where I am trying to use a custom background color for the Markdown block (e.g. light gray).

Ideally the solution is some sort of macro, e.g. mymarkdown.

\documentclass{article}

\usepackage{markdown}

\usepackage{lipsum}  

\begin{document}

\lipsum[1]

\begin{markdown}
This is now inside Markdown

## Here is a Markdown list
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
  - Marker character change forces new list start:
    * Ac tristique libero volutpat at
    + Facilisis in pretium nisl aliquet
    - Nulla volutpat aliquam velit
+ Very easy!

## Here are some Markdown blockquotes
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.][1]][1]


\end{markdown}

This is now outside of Markdown
\begin{itemize}
    \item Item 1
    \item Item 2
    \item Item 3
\end{itemize}
\end{document}

enter image description here

3
  • 1
    Why not just put it all in a tcolorbox environment? Feb 23, 2020 at 4:16
  • 1
    @DavidPurton It seems not be as easy as you say (if, as I guess, the OP will create a new environment for his task). With \newenvironment{colormarkdown}{\begin{tcolorbox}\begin{markdown}}{\end{markdown}\end{tcolorbox}} and \begin{colormarkdown} ... \end{colormarkdown}, you will get LaTeX Error: \begin{tcb@savebox} on input line ... ended by \end{document}. and the compilation ended with ! Emergency stop. and ! ==> Fatal error occurred, no output PDF file produced!.
    – quark67
    Feb 26, 2020 at 1:24
  • @quark67, it is a pain to create a new environment because the markdown environment looks for an explicit \end{markdown} and this will never occur but you can just do \begin{tcolorbox}\begin{markdown}...\end{markdown}\end{tcolorbox}. Feb 26, 2020 at 11:07

1 Answer 1

10

You can use tcolorbox and coerce markdown into opening a tcolorbox before doing its business, issuing \end{tcolorbox} at the end.

Add the desired options to the starting \begin{tcolorbox} (look at the very detailed manual).

\documentclass{article}

\usepackage{markdown}
\usepackage{tcolorbox}
\usepackage{etoolbox}

\pretocmd{\markdown}{\begin{tcolorbox}}{}{}
\def\endmarkdown{\end{tcolorbox}}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

%\begin{tcolorbox}
\begin{markdown}
This is now inside Markdown

## Here is a Markdown list
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
  - Marker character change forces new list start:
    * Ac tristique libero volutpat at
    + Facilisis in pretium nisl aliquet
    - Nulla volutpat aliquam velit
+ Very easy!

## Here are some Markdown blockquotes
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.][1]][1]


\end{markdown}
%\end{tcolorbox}

This is now outside of Markdown
\begin{itemize}
    \item Item 1
    \item Item 2
    \item Item 3
\end{itemize}
\end{document}

enter image description here

You must log in to answer this question.

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