How to pass text with # in the beginning to a macro (minted)?

If you define

\newcommand{\inlinecode}[2][text]{%
  \mintinline{#1}{#2}%
}

then upon calling \inlinecode{...}, the argument is tokenized before \mintinline can do the changes to the standard interpretations of special characters it needs (it is a special verbatim mode).

Just change the definition into

\newcommand{\inlinecode}[1][text]{%
  \mintinline{#1}%
}

Now \mintinline can do its job and absorb itself the argument still not tokenized.

Note that you can also use

\inlinecode|text|

with this definition.

Full example

\documentclass{scrartcl}

\usepackage{minted}

\newcommand{\inlinecode}[1][text]{%
  \mintinline{#1}%
}

\begin{document}%
  text \inlinecode{somecode} text
  text \inlinecode{#pragma} text
  text \mintinline{text}{#pragma} text
\end{document}

enter image description here

What happens? When TeX sees (the new) \inlinecode it scans ahead to see whether [ is next. If no [ is found, then \mintinline{text} replaces \inlinecode, so the next step will be processing

\mintinline{text}{#pragma}

With a call such as \inlinecode[cpp]{#pragma}, the [ is scanned, so the definition of the macro makes the replacement into \mintinline{cpp}; eventually

\mintinline{cpp}{#pragma}

will be processed.