2

I put some text into a box using \tcbox. I then intend to create a new line in the box as follows:

\tcbox{%
        \begin{small}
            \begin{concmath}
                first line\\ % here a new line is expected in the box. 
                second line. \\ \\
            \end{concmath}
        \end{small}\\
    }% 

But, new line is not created in the box.

P.S. concmath is used to change the font, as it is need. Font change is defined as follows:

\usepackage[T1]{fontenc}
\DeclareMathVersion{concmath}
\SetMathAlphabet{\mathrm}{concmath}{\encodingdefault}{ccr}{m}{n}
\newenvironment{concmath}
{\fontfamily{ccr}\selectfont\mathversion{concmath}}
{\ignorespacesafterend}

2 Answers 2

2

Use option tikznode. It is documented near the end of tcolorbox documentation, sec. 4.12. For example,

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{tikz}

\begin{document}
\tcbox[tikznode]{%
  \small
  text \\ text
}
\end{document}

enter image description here

Update

With

\tcbox[tikznode]{%
        \begin{small}
            \begin{concmath}
                first line\\ % here a new line is expected in the box. 
                second line. \\ \\
            \end{concmath}
        \end{small}\\
    }% 

error

! Extra }, or forgotten \endgroup.
\tikz@@align@newline [#1]->\egroup 
                                   \tikz@align@continue \pgfmathparse {#1}\l...

is raised. This is reproducible with tikz only, when you input multiline node text inside simple environment, as in

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikz \node[align=center] {
  \begin{small}
    first line\\ % here a new line is expected in the box. 
    second line. \\ \\
  \end{small}
};
\end{document}

I think this is caused by the redefinition of \\ inside node text and extra group introduced by simple environment.

Since both small and concmath environment do font settings, you can move their effect into some font option. For example,

\documentclass{article}

\usepackage[T1]{fontenc}
\DeclareMathVersion{concmath}
\SetMathAlphabet{\mathrm}{concmath}{\encodingdefault}{ccr}{m}{n}
\newenvironment{concmath}
  {\fontfamily{ccr}\selectfont\mathversion{concmath}}
  {\ignorespaces}

\usepackage{tcolorbox}
\usepackage{tikz}

\newtcbox{\xtcbox}{
  tikznode,
  fontupper=\small\fontfamily{ccr}\selectfont\mathversion{concmath}  
}

\begin{document}
\xtcbox{%
  first line\\ % here a new line is expected in the box. 
  second line. \\ \\
}
\end{document}

Moreover, to keep the use of environments small and concmath, you can nest the box content in a varwidth environment, which is defined by varwidth package.

\documentclass{article}

\usepackage[T1]{fontenc}
\DeclareMathVersion{concmath}
\SetMathAlphabet{\mathrm}{concmath}{\encodingdefault}{ccr}{m}{n}
\newenvironment{concmath}
  {\fontfamily{ccr}\selectfont\mathversion{concmath}}
  {\ignorespaces}

\usepackage{tcolorbox}
\usepackage{varwidth}

\begin{document}
\tcbox{%
  \begin{varwidth}{\linewidth}
    \begin{small}
        \begin{concmath}
            first line\\ % here a new line is expected in the box. 
            second line. \\ \\
        \end{concmath}
    \end{small}\\
  \end{varwidth}
}
\end{document}
5
  • Thank you, it works without using changing font, but when use of tikznode with concmath (for changing font) I got an error for the number of } ... Is there a solution for this ?
    – Questioner
    Commented Jun 18, 2020 at 17:04
  • @Questioner How concmath environment is defined? Maybe you should provide a complete example, also called mwe. Commented Jun 18, 2020 at 17:08
  • I added how to define font change in the question. If you need any complementary information please let me know. Thanks
    – Questioner
    Commented Jun 18, 2020 at 17:11
  • Meanwhile, if you know an alternative to tcbox that works better for this purpose, please inform me. Thanks
    – Questioner
    Commented Jun 18, 2020 at 17:16
  • 1
    @Questioner Answer updated. Commented Jun 18, 2020 at 18:10
0

The simple answer is to use tcolorbox instead of \tcbox, as follows:

\begin{tcolorbox}
        \begin{small}
            \begin{concmath}
                first line\\ % here a new line is expected in the box. 
                second line.
            \end{concmath}
        \end{small}
\end{tcolorbox}
1
  • 4
    tcolorbox and \tcbox are different in how the width of output box is determined. The width of tcolorbox is given. By default the box width is \linewidth. The width of \tcbox is determined by its content. Commented Jun 18, 2020 at 18:19

You must log in to answer this question.

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