2

I am trying to convert **sometext** to bold. I came across the following snippet:

\makeatletter
\def\starparse{\@ifnextchar*{\bfstarx}{\itstar}}
\def\bfstarx#1{\@ifnextchar*{\bfitstar\@gobble}{\bfstar}}
\makeatother
\def\itstar#1*{\textit{#1}\starON}
\def\bfstar#1**{\textbf{#1}\starON}
\def\bfitstar#1***{\textbf{\textit{#1}}\starON}
\def\starON{\catcode`\*=\active}
\def\starOFF{\catcode`\*=12}
\starON
\def*{\starOFF \starparse}
\starOFF

that could help me transorm a text wrapped with ** on both sides into bold text. But it also transforms *sometext* into italics. I only want to detect double asterisks and transform it into bold.

I tried removing :

\def\itstar#1*{\textit{#1}\starON}

but it gives a undefined controlled sequence error. How could I do this?

  • I thought there are some packages help you use Markdown inside LaTeX? – user156344 May 10 at 9:05
  • @JouleV Yes, there is. Could we do this without using the markdown package? – Amanda May 10 at 9:26
  • @Amanda Yes, the code implements markdown syntax, like in this site: *italic*, **bold**, and ***bold-italic***. What should be the output of ***bold-italic***? *bold-italic*, bold-italic, or ***bold-italic***? – Phelype Oleinik May 10 at 10:22
  • @PhelypeOleinik It must go undetected. Only text surrounded with ** must be considered. – Amanda May 10 at 10:37
2

Changed the code a bit so that:

\starON
*italic*

**bold**

***bold-talic***
\starOFF

prints:

enter image description here

The code does no verification whatsoever to guarantee that the input is not something like **bold***. When the code finds a * it checks if there is exactly one * more (ignoring spaces), and if there is it grabs everything until the next ** (no spaces allowed between *s), so * *bold** is valid, but **bold* * is not.

\documentclass{article}
\makeatletter
\def\starparse{\@ifnextchar*{\bfstarx}{\starignore}}
\def\bfstarx#1{\@ifnextchar*{*\starignore}{\bfstar}}
\def\starignore{*\starON}
\makeatother
\def\bfstar#1**{\textbf{#1}\starON}
\def\starON{\catcode`\*=\active}
\def\starOFF{\catcode`\*=12}
\starON
\def*{\starOFF\starparse}
\starOFF

\begin{document}

\starON
*italic*

**bold**

***bold-talic***
\starOFF

\end{document}

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.