2

I use datatool to get data and row spacing can change.

Count word/character of Total character of (Text A + Text B) if:

Total charater of (Text A + Text B) < 20 characters => set row space of text in tcolorbox = 1.5 cm

Total character of (Text A + Text B) = 21 - 50 characters => set row space of text in tcolorbox = 0.8 cm

Total character of (Text A + Text B) > 50 charaters => set row space of text in tcolorbox = 0.3 cm

Minimal working code:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{wrapfig}
\usepackage{lipsum}
\tcbset{colframe=blue!75!black,colback=white}
\usepackage{filecontents}
\begin{filecontents*}{file.tex}

No,TextA,TextB
1,a a a a a a a a a a a a a a a a a a a a,b b b b b b
2,a a a a a a a a a a,b b b b b b 
3,a a a a a a a a a a,b b b b b b b b b b b b
4,a a a a a a a a a a,b b b b b b b b b b b b b b b b b b
5,a a a a a a a a a a a a a a a,b b b b b b 
6,a a a a a a a a a a a a a a a a a a a a,b b b b b b 
7,a a a a a a a a a a a a a a a,b b b b b b  b b b b b b

\end{filecontents*}
\usepackage{datatool}
\DTLloaddb[autokeys=false]{file}{file.tex}

\begin{document}

\DTLforeach*
    {file}% Database
    {\No=No,\TextA=TextA,\TextB=TextB}{%

    %\begin{mybox}[height=4.5cm]
   \begin{tcolorbox}[height=4cm,width=6cm]


\begin{wrapfigure}{R}{1.5cm}
\centering
\includegraphics[width=2cm,height=2cm]{example-image-a}
\end{wrapfigure}
\No \quad \TextA \quad \TextB\par
\end{tcolorbox}
\newpage
  }% 
\end{document}

Many thank

Bernard
  • 271,350
latexforti
  • 2,091
  • 1
    tcolorbox offers fitting library to do the inverse task, you can reduce fontsize to fit certain text inside a defined box. May be you could consider it as an alternative solution. – Ignasi Apr 08 '19 at 08:03
  • @Ignasi. thank for your solution. i see this https://tex.stackexchange.com/questions/271829/fit-text-within-a-frame-with-fixed-dimension-shrink-only-if-needed . but it's Rectangle shape. My situation is L shape (with \includegraphics). thanks – latexforti Apr 08 '19 at 08:53
  • 1
    Yes, I know it's only valid for rectangular shapes but as your example was so short it was not clear that the L shape was so necessary. – Ignasi Apr 08 '19 at 10:36

1 Answers1

2

You have to set the baseline skip before wrapfigure.

\RequirePackage{filecontents}

\begin{filecontents*}{\jobname-file.tex}

No,TextA,TextB
1,a a a a a a a a a a a a a a a a a a a a,b b b b b b
2,a a a a a a a a a a,b b b b b b 
3,a a a a a a a a a a,b b b b b b b b b b b b
4,a a a a a a a a a a,b b b b b b b b b b b b b b b b b b
5,a a a a a a a a a a a a a a a,b b b b b b 
6,a a a a a a a a a a a a a a a a a a a a,b b b b b b 
7,a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a,b b b b b b  b b b b b b b b b b b b  b b b b b b b b b b b b  b b b b b b

\end{filecontents*}

\documentclass[twocolumn]{article}
\usepackage[many]{tcolorbox}
\usepackage{filecontents}
\usepackage{datatool}
\usepackage{wrapfig}
\usepackage{xparse}

\usepackage{lipsum}

\tcbset{colframe=blue!75!black,colback=white}

\DTLloaddb[autokeys=false]{file}{\jobname-file.tex}

\ExplSyntaxOn
\NewDocumentCommand{\setbaselineskip}{mmm}
 {
  \tl_set:Nx \l_tmpa_tl { \exp_not:V #1 ~ \exp_not:V #2 ~ \exp_not:V #3 }
  \int_compare:nTF { \tl_count:N \l_tmpa_tl <= 20 }
   {
    \setlength{\baselineskip}{ \dim_eval:n { \baselineskip+1.5cm} }
   }
   {
    \int_compare:nTF { \tl_count:N \l_tmpa_tl <= 50 }
     {
      \setlength{\baselineskip}{ \dim_eval:n { \baselineskip+0.8cm} }
     }
     {
      \setlength{\baselineskip}{ \dim_eval:n { \baselineskip+0.3cm} }
     }
   }
 }
\ExplSyntaxOff

\begin{document}

\DTLforeach*
  {file}% Database
  {\No=No,\TextA=TextA,\TextB=TextB}
  {%
   \begin{tcolorbox}[width=6cm]
   \setbaselineskip{\No}{\TextA}{\TextB}
   \begin{wrapfigure}{R}{2cm}
     \centering
     \includegraphics[width=2cm,height=2cm]{example-image-a}
   \end{wrapfigure}
   \No \quad \TextA \quad \TextB
   \end{tcolorbox}
%   \newpage
  }

\end{document}

enter image description here

egreg
  • 1,121,712
  • Sorry my fault. Row spacing = Line spacing = space between 2 rows . https://imgur.com/FWVzQQA. Thank you, can you edit your code for line spacing, not for word spacing. Thank – latexforti Apr 08 '19 at 11:27
  • 1
    @latexforti Fixed – egreg Apr 08 '19 at 11:43
  • thank you. it's awesome. Now i don't want display \No in text. How can i edit code without No, only Total of \textA & \textB. \setbaselineskip{\TextA}{\TextB} thanks – latexforti Apr 08 '19 at 22:17