1

(Xelatex) How can one determine the maximal line length (e.g. in characters) of a external, plain text file? This file likely contains special Latex characters.

Usecase is syntax highlighted code by minted, which should be stored as external files. Additionaly those minted code snippets stand in a mdframed, minipage, center environment.

So the code should be centered and background should fit to the maximal length of one line. In my example one would always need to adjust the e.g. 12cm.

I was not able to determine the width of minted code. My approach for a work around is to use the pure character count.

\documentclass[a4paper]{article}

\usepackage{minted}
\usepackage{mdframed}

\begin{document}

\begin{center}
\begin{minipage}{12cm}
\begin{mdframed}[backgroundcolor=green!10!white, userdefinedwidth=12cm]
\inputminted{python}{example.py}
\end{mdframed}
\end{minipage}
\end{center}

\end{document}

I've tried:

  • calc: \widthof
  • \input
  • verbatim: \verbatiminput
  • stringstrings: \stringlength

It was not possible to use \widthof and \stringlength for strings which contain special Latex characters, like: {}. Verbatim is not working inside those macros.

2
  • You could maybe grab the longest line from the file via wc -L and then, having multiplied it by a factor of your choosing, input that into a length command, which you then do with userdefinedwidth=\mylongline.
    – jon
    Mar 16 '16 at 21:23
  • I also thought about solving it outside of Latex. That would be some workaround. But I rather would like it to work fully automatic. So when I change the external sourcecode files and recompile the Latex file, everything should fit fine. Mar 16 '16 at 22:17
1

Couldn't help expanding on my earlier comment....

Here's a simple example, which you can compile with -shell-escape:

\RequirePackage{filecontents}
\begin{filecontents*}{example.py}
#!/usr/bin/env python
Celsius = int(raw_input("Enter a temperature in Celsius: "))

Fahrenheit = 9.0/5.0 * Celsius + 32

print "Temperature:", Celsius, "Celsius = ", Fahrenheit, " F"
\end{filecontents*}
\documentclass[a4paper]{article}
\parindent0pt
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{mdframed}

% the data file
\def\keyfile{example.py}

% find the longest line and print value to `longline.wc`
% compile with `-shell-escape` option
\immediate\write18{wc -L \keyfile | awk '{print $1}'  > longline.wc} %$

% read in value to \longlineval
\newread\longline
\openin\longline=longline.wc
\read\longline to \longlineval

% convert value from longline.wc to a 'pt' value
\newlength\longlineconv
\longlineconv=\longlineval pt
% multiply by a chosen factor (here: 5.25)
\def\mfactor{5.75}
\newlength\mdlen
\setlength\mdlen{\dimexpr \mfactor\longlineconv \relax}

\begin{document}

Longest line of ``\keyfile'': \longlineval\par
Multiplication factor: \mfactor\par
Result of multiplication factor: \the\mdlen\par

\bigskip

% Using the length
\begin{center}
\begin{minipage}{12cm}
\begin{mdframed}[backgroundcolor=green!10!white, userdefinedwidth=\mdlen]
\inputminted{python}{example.py}
\end{mdframed}
\end{minipage}
\end{center}

\end{document}
2
  • I didn't know about the possibility of calling shell code. I should had written --shell-escape more attentive because I certainly have to call it. Sorry, so your comment actually solves the problem! Mar 17 '16 at 10:12
  • Glad it works for you!
    – jon
    Mar 17 '16 at 14:51

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.