2

The mybox command is from this answer.

Original mybox

\newcommand\mybox[2][]{\tikz[overlay]\node[fill=black!20, inner sep=2pt, 
                        anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}

If I use this command, I get results like below

enter image description here

I want it to have a bit more code like style, So I made it like below

\newcommand\mybox[2][]{\tikz[overlay]\node[fill=black!20, inner sep=2pt, 
                        anchor=text, rectangle, rounded corners=1mm,\texttt{#1}] {#2};\phantom{#2}}

This one didn't even compile, so I searched and adjusted it to

\newcommand\mybox[2][]{\tikz[overlay]\node[fill=black!20,font=\ttfamily, inner sep=2pt, 
                        anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}

This one compiled, but it wasn't what I wanted enter image description here So I made another command to wrap it.

\newcommand\mybox[2][]{\tikz[overlay]\node[fill=black!20, inner sep=2pt, anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}
\newcommand{\incode}[2][]{\mybox[#1]{\relscale{0.85}\texttt{#2}}}

enter image description here

I added some relative scaling because the font was bigger than I wanted it to be. The result I got is exactly what I want. However, I want to make these two commands in to one. Some help will be nice.

EDIT: MWE added

\documentclass[12pt, a4paper]{report}
\usepackage{tikz}
\usepackage{relsize} % relative scaling for font size

\newcommand\mybox[2][]{\tikz[overlay]\node[fill=black!20, inner sep=2pt, anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}} \newcommand{\incode}[2][]{\mybox[#1]{\relscale{0.85}\texttt{#2}}}

\begin{document} Example \incode{0.2example} example \end{document}

Moses Kim
  • 183
  • 1
    It will more easy to answer you, if you will provide an MWE with use of your commands. Now, if one like to help you, is forced to write test document from scratch :-(. Help us to help you :-) – Zarko Feb 11 '22 at 15:32
  • @Zarko Thanks for the input! I'll the MWE to my question. – Moses Kim Feb 11 '22 at 15:39

2 Answers2

1

Thank you for adding MWE!

A possible solution is simple: insert definition of second command to the first:

\documentclass{article}
\usepackage{relsize}
\usepackage{tikz}
\newcommand\mybox[2][]{\tikz[overlay]
    \node[rounded corners=1mm, fill=black!20, inner sep=2pt, anchor=text,
          #1] {\relscale{0.85}\texttt{#2}};\phantom{#2}}

\begin{document} some text \mybox{0.2example} some text \end{document}

enter image description here

Edit: and example to use option of command:

\begin{document}
some text \mybox[draw=red]{0.2example} some text
\end{document}

enter image description here

Zarko
  • 296,517
1

An alternative with tcolorbox

\documentclass[12pt, a4paper]{report}

\usepackage[most]{tcolorbox}

\newtcbox{\incode}[1][]{on line, enhanced, colback=black!20, fontupper=\sffamily\small, boxrule=0pt, boxsep=2pt, left=2pt, right=2pt, top=2pt, bottom=2pt, frame hidden,#1}

\begin{document} Example \incode{0.2example} example \incode[colback=red!30]{0.2example} \end{document}

enter image description here

Ignasi
  • 136,588