LONDON — President Trump did not act like a man about to be honored by the queen — until he came face to face with the 93-year-old British monarch Monday afternoon.

For three hours between his morning arrival at Stansted Airport and a formal welcoming ceremony just after noon, Trump set a cantankerous tone for his five-day European trip, tweeting out a series of attacks and complaints.

In a city and country where he is already deeply unpopular, the president at times personified a familiar stereotype: the rude American abroad.

Trump was, by contrast, gracious in the presence of Queen Elizabeth II, whom he praised as the embodiment of “duty and patriotism.” But even in Buckingham Palace, the toasts at the evening’s state dinner carried an unmistakable hint of tension into the gilded Banquet Hall.

Days away from the 75th anniversary of D-day, both the president and the queen lauded the shared history of the Allies’ victory. And they praised shared values. But their points of emphasis noticeably differed.

Trump emphasized “freedom, sovereignty, self-determination [and] the rule of law” as common values. The queen spoke about the importance of the postwar alliances that many here and elsewhere in Europe — and in the U.S. — say Trump has seemed intent on weakening.

“After the shared sacrifices of the Second World War, Britain and the United States worked with other allies to build an assembly of international institutions, to ensure that the horrors of conflict would never be repeated,” she said. “While the world has changed, we are forever mindful of the original purpose of these structures: nations working together to safeguard a hard-won peace.”

For the monarch, who by tradition and habit avoids political controversy, the language seemed pointed.

If Trump took the point, he did not show it. Seated beside the queen in a black tuxedo with a white bow tie, Trump appeared to enjoy the ceremony. During his time away from his hosts, however, he remained focused on politics — and perceived enemies, domestic and foreign.

Before Air Force One had touched down, Trump attacked London’s mayor, Sadiq Khan, as a “stone cold loser.”

In a second tweet, he likened Khan to “our very dumb and incompetent Mayor of NYC [Bill] de Blasio” and mocked the London mayor’s height — he’s about 5 feet 6 — before noting that he was glad to be visiting.

“In any event, I look forward to being a great friend to the United Kingdom, and am looking very much forward to my visit. Landing now!” he wrote.

After being flown from the airfield northeast of London to Winfield House, the U.S. ambassador’s residence here, for some downtime ahead of afternoon ceremonies, Trump continued to tweet, claiming that tariffs were forcing China to pay “billions” to the U.S. and carping about his limited television options.

“Just arrived in the United Kingdom,” he tweeted. “The only problem is that @CNN is the primary source of news available from the U.S. After watching it for a short while, I turned it off. All negative & so much Fake News, very bad for U.S. Big ratings drop. Why doesn’t owner @ATT do something?”

That led to a second tweet in which Trump appeared to recommend a boycott of a major U.S. company, saying that if people “stopped using” AT&T — a company that employs more than 100,000 Americans — CNN might be “forced to make big changes.”

Once the ceremonies began outside Buckingham Palace, however, the president was all smiles as Marine One delivered him and First Lady Melania Trump to the grand lawn, where he was greeted by Prince Charles and Camilla, Duchess of Cornwall, and led up the steps to the greet the waiting queen.

Cannons fired in a ceremonial welcome salute, and dignitaries stood and watched as the Grenadier Guards, wearing the traditional red coats and bearskin hats, marched in formation before playing the U.S. national anthem.

At one point, Trump and Prince Charles walked among the guards for an official inspection.

Jared Kushner, Ivanka Trump, Treasury Secretary Steven T. Mnuchin, senior advisor Stephen Miller and U.S. Ambassador Woody Johnson were among those watching the ceremonies from a palace balcony.

The queen presented Trump with the abridged, single-volume version of Winston Churchill’s multibook series, “The Second World War,” as well as a boxed set of three royal pens. It’s commonly known that Trump seldom reads books.

To some watching back in the U.S., the queen’s steadfastly apolitical and polite persona stood in stark contrast to her guest of honor, however well mannered he appeared in her presence.

“An important Presidential responsibility is to represent the American people overseas with dignity,” tweeted R. Nicholas Burns, a former U.S. ambassador to the North Atlantic Treaty Organization. “Trump has failed this straightforward duty in the first hour of his State Visit to the U.K. How pitiful.”

Trump’s day of official festivities also included a private lunch with the queen, a visit to Westminster Abbey and the Tomb of the Unknown Warrior, and tea with Prince Charles and Camilla.

The president briefly went back to Winfield House to rest before returning to the palace for the official state banquet and a reception to follow, and almost immediately returned to Twitter with an announcement that the trip was “going really well” and a bleat about the negative media coverage he was anticipating.

“The Queen and the entire Royal family have been fantastic,” Trump tweeted. “The relationship with the United Kingdom is very strong.”

“Haven’t seen any protests yet, but I’m sure the Fake News will be working hard to find them,” he wrote. “Great love all around. Also, big Trade Deal is possible once U.K. gets rid of the shackles. Already starting to talk!”

Advocates of Brexit have long argued that Britain could sign a separate trade deal with the U.S. if it pulls out of the European Union. Skeptics in Britain question whether Trump can be trusted to negotiate a deal and stick to it.

The pageantry and pleasantries somewhat mask Trump’s deep unpopularity with much of the British population. According to a new poll, fewer than 1 in 5 people in Britain believe the United Kingdom still has “the special relationship” with the U.S. that this state visit is meant to commemorate and solidify.

Trump has celebrated the country’s 2016 Brexit vote. But even if the U.K. turns away from Europe by leaving the EU and negotiating its own trade deal with the U.S., significant daylight still separates the two nations, which have taken opposite positions on climate change and the Iran nuclear deal, among other things.

Khan took advantage of the international platform the president’s attack provided him, releasing a two-minute video in which he spoke directly to the camera — and the visiting American.

“If you’re watching this, your values and what you stand for are the complete opposite of London’s values and the values in this country,” Khan said. “We think diversity is a strength. We respect women. And we think they’re equal to men. We think it’s important to safeguard the rights of all of us, particularly vulnerable and the marginalized.”

Special correspondent Christina Boyle contributed to this report.