Flattening LaTeX source files for use with conversion software

I’ve been working with LaTeX quite a bit recently and I came across a very silly shortcoming that some of the programs that convert LaTeX to other formats suffer from. It’s moronic, but certain of them (pandoc, I’m looking at you), don’t follow includes (i.e. \input commands).

I have a PHP script which compiles all my LaTeX source files into various destination formats, including PDF, HTML, MediaWiki markup and text, and so in order to compensate for this ridiculous incapability I added this function which creates a “flattened” version of any LaTeX file by inserting the contents of any included files at the point they are included. It’s recursive, so if those included files also include files, they too are processed.

It’s simple but effective. Pass the path to the file to flatten. A string is returned with the entire flattened file. You may need to pay attention to relative paths in your \input commands if you includes are in sub-directories.

function FlattenLatexSourceFile($path) {
	// read in file
	$src = file($path);
	$flattened = "";

	// process each line
	foreach ($src as $line) {
		if (preg_match("/^\\\input/",$line)) {

			// include (input) line, extract filename and recurse
			$input = trim($line);
			$input = preg_replace("/^\\\input\{/","",$input);
			$input = preg_replace("/\}/","",$input);
			if (substr($input,-4,4) != ".tex") $input = "{$input}.tex";
			$flattened .= "% {$line}\n";
			$flattened .= FlattenLatexSourceFile($input);
			$flattened .= "\n% (end of included file {$input})\n";
		} else {
			// normal line
			$flattened .= $line;
	// return flattened file
	return $flattened;


Mountain biking renaissance

Trek Antelope 830

The only way you can tell it's 19 years old is its lack of technology.

When I was sixteen my parents bought me a new mountain bike as a congratulations present for passing my GCSEs. It’s a Trek Antelope 830, an American design which was, so I’m told by the grease monkeys in Halfords, something of a classic model from the early 1990s. It’s seen little use since I learnt to drive and languished in the car park of the apartment building in Manchester for a full seven years before I moved to Sutton Coldfield, disused and neglected.

I’d been meaning to get it fixed up for some time ever since moving because almost on my doorstep I have the largest urban park in Europe, Sutton Park, and the place is perfect for spending an afternoon biking around. It has roads and bridleways and once you’re bored of those it has hundreds of square miles of terrain to explore. So after having earned a bit of overtime during a busy period at work recently I finally got around to taking it to the local branch of Halfords to get it fixed up. For £160 I secured the following:

  1. New gear shifters.
  2. New saddle and saddle column. The last one “went missing” years ago from the W3 car park.
  3. New rear inner tube. Surprised that it didn’t need both tubes replacing, actually.
  4. Full service, plus another free service in 12 months time.
  5. New cycle helmet.
  6. Front and rear LED lights.
  7. Bicycle pump. They look completely different these days to what they used to.

The original gear shifters were broken and the parts were no longer available. The staff at Halfords remarked on these because they remember when that particular model of shifter first came out as they were apparently one of the first 7×3 shifters. They have been replaced with modern equivalents. This is a shame because it means that the bike is no longer completely original, but they are of course a necessity if I want to actually use it.

So yesterday I took it for its first proper outing to Sutton Park and I had a fabulous time. The bike’s great to ride, despite having a distinct and noticeable lack of modern technology that seems to be ubiquitous on modern bikes, most notably suspension and shock absorbers. As a result it’s a bit of a bone shaker when compared to a modern bike. It also has the old fashioned rim brakes, whereby the brakes are rubber blocks applied to the rim of the wheel, as opposed to the more modern disc brakes, which look like and act on the same principle as car brakes. This means that the bike can be a little tricky to stop quickly in an emergency. However,  I never intend to have it in a situation so extreme that I cannot easily plan my way out of it. I know the bike has limitations compared to a modern equivalent and it’s just a case of taking them into account.

The bike rides fine, just like it used to, except the chain seems to slip quite frequently and it’s difficult to get it onto the third (largest) cog on the front sprocket. Indeed, at one point it failed so miserably to do this that the chain came off and became wrapped around the pedal shaft, which required half an hour with the bike up on end and some very oily hands to work out and sort out. I’m not sure what might be the solution to this. They didn’t replace the chain at Halfords so it may well be the case that it’s just a bit old and needs replacing. I am consulting friends who are more into mountain biking for advice on the matter.

Lessons I have immediately learned from this first expedition are as follows:

  1. Take more than a litre of water for an afternoon. You get thirsty really quickly.
  2. Take a pair or two of those medical rubber gloves. When the chain came off my hands got absolutely covered in oil fixing it and I had no way of getting it off until I reached the bistro where I had my late lunch.
  3. There is barely any mobile signal in Sutton Park and so it’s difficult to use Google Maps. Sutton Park is extremely large and will take some time to learn properly.
  4. Skiing sunglasses make perfectly good cycling glasses and don’t look absurd with a cycling helmet.
  5. Cycling helmets don’t look even a fraction as ridiculous as they used to in the early 1990s.
  6. If I ever become reasonably serious about mountain biking I do think that I might have to consider a new bike at some point as I have a very sore arse today and this would probably have been avoided had the bike had some form of shock absorption technology.

Today my legs are absolutely killing me, as is my arse, but I fully intend to do exactly the same trip next Saturday, weather permitting. Ahead of that, however, I want to by a trip meter of some sort and some sort of attachment to hold my phone onto the handlebars would also be beneficial. Some people have also recommended padded shorts to reduce the arse soreness problem, but I won’t be seen dead in cycling shorts, it would have to be something I can wear underneath normal cargo shorts. I’d also probably do well to get a pair of gloves as I remember mashing my hands up quite significantly when I came off the bike as a teenager.

Cows in Sutton Park

Sutton Park is a nature reserve and boasts much wildlife, including these cows which are free and have the run of the park. They are apparently quite used to being around humans.